Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Website developers who are accustomed to using Cascading Style Sheets to affect the appearance of their HTML elements might be used to using ID selectors. An ID selector is a Cascading Style Sheet selector that applies its properties to only one element of the page matched by the ID. For example, in the current version of the header control the panel with an ID of menuPanel, which is wrapped around the anchor tags that make up the menu, is identified by the phrase menuPanel. I have used the Cascading Style Sheet class of the same name but they aren't related to each other.
If I wanted to modify this panel and I wanted to apply it using an ID selector, I would go back to styles.css and instead of putting a period, I would put in the pound sign. Now I'm taking about the menuPanel ID, rather than the menuPanel class. There is a problem with this approach in ASP.NET that you must know about. When you had cleared an ASP.NET server control such as asp:panel and you place it within a custom control, such as this Header control, at one time the ID is rewritten using a combination of the ID of the control in the page plus the ID of the element within the control.
I am going to demonstrate this by modifying styles.css to use the pound sign instead of the dot. Meaning, I'm matching by ID rather than by class. Then I'll go back to the page Default.aspx and run it in the browser. Notice that the appearance of the menu, including the border around it, the padding and so on, that I had to clear using the class selector that no longer applies. Here's why. I will right click anywhere on the page and select View Source. Then I'll scroll down to the generated code for the panel. Here it is. It's a Div tag with an ID. You will see that the ID consists of the ID of the control instance in the page, which was header 1, and the ID of the control itself within the server control which was menuPanel.
So, they are not matching up with each other. I'll close the text editor and the browser. I'll also show you where the prefix of the ID is coming from. From the declaration of the ID in the instance of the header control. So if you want to use ID selectors to match up your HTML element Ids with Cascading Style Sheet rules, here is what you have to do. I will go back to styles.css and I'll modify my selector to match what will be generated by .NET. header1_menuPanel. I'll save those changes by pressing Ctrl+S, go back to the page and run it again. This time when the menu appears I see the styles kick in, including the border and the padding that I had declared.
Now that I'm matching up by ID, I can go back to the Header file and I no longer need the CSSClass declaration. I'm matching up the menu selector by a combination of the top-level page's ID as it declares the instance of the server control plus the id of the menuPanel. The problem with this approach is that you now have to make sure that you are using exactly the same declaration ID in every single page. If you change it, you will have differences from one page to another. So therefore, my preference when working with ASP.NET server controls, especially those that are embedded within custom controls, is to use class selectors.
These won't be modified at runtime by ASP.NET. So I'll return all these pages to their original settings. I'll put the CSSClass attribute back inside the Header.ascx file. I'll go back to styles.css and change my selector back to a class selector. I'll save all my changes by selecting File > Save All. Go back to the page and run it one more time without debugging. See that the design of the pages is still intact. So again, rule of thumb. When you declare controls within custom controls and you want to apply Cascading Style Sheets to them on a global basis throughout the website, use Cascading Style Sheet classes rather than Ids for the best long term maintainability of your website.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97442 Viewers
61 Video lessons · 84668 Viewers
71 Video lessons · 68871 Viewers
56 Video lessons · 101326 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.