Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Once you have created a user control, you can then use it in many pages. There are two basic approaches that you can select from. In one, you explicitly register the control in each page in which you want to use it. And then using an ASP.NET tag, you display the control. The other approach is to register the control globally for the whole website. Here, I'm going to demonstrate how to register the file locally. As I have shown in another video, you can actually drag and drop a control into a page in Design View, and all of the required code will be generated for you, but it's useful to know how to hand code it where necessary.
I am working with the file Authors.aspx, which has the same sort of markup as the Default.aspx file I used previously. I'm going to place the cursor after the page declaration, but before the html DOCTYPE declaration, and I'll make a little bit of extra space. I'll start with a less than character and a percent character, and you'll see a list of available declarations appear. I'll go down to the Register declaration, and I'll select it and then press the spacebar. You can register a control either from the source file which is what we're working with here, or if you are working with pre-compiled code, you can register a control from an Assembly and Name space.
I am going to use the Source attribute, because I'm referring to the control by its source file name. I'll select source, Src. I'll put in the equal sign and a quote, and then I'll double-click to select Pick URL. Then I'll navigate to the Controls folder and I'll choose my file Header.ascx and click OK. Notice that Visual Web Developer sets the source using a tilde before /controls. The expression tilde slash refers to the root of the website.
Next, I declare a TagName and a TagPrefix. These can be anything I like. I'll press the spacebar and then select TagName, put in the equals and a quote, and I'll set the TagName as Header. Then I'll press the spacebar again and select TagPrefix, type in the equals and a quote, and I'll set the Prefix as uc1 for user control folder 1. Notice that after a moment, all of the little squigglies that indicate errors have gone away. This is a visual indicator that says I have coded this correctly.
Now, I'll go down further in the page and I'm going to select the tags that present the image and the menu. And I'll delete them by pressing the Delete key. Then I'll tab in so that I'm aligning the cursor with the existing code. I'll type in a less than character and type UC and you'll see that uc1:Header is displayed by Visual Web Developer as a possible option. I'll select the item, and I'll also add runat = server. That part is critical. If you don't put in the runat="server" attribute, then when you run the page, your header won't appear because it won't have been processed at the server level.
I'll save my changes and test the page. Selecting Debug > Start Without Debugging. When the page appears, it once again displays the image and the hyperlinks. I can click to go to the homepage and back to the Author's page, and in both cases, I'm using exactly the same user control, but the details of the page below the control are different.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97479 Viewers
61 Video lessons · 84712 Viewers
71 Video lessons · 68907 Viewers
56 Video lessons · 101355 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.