Home » ASP.NET » Dynamically Change class Attribute From ASP.NET

Dynamically Change class Attribute From ASP.NET

B03B0015 I recently received a question from another programmer I know who’s been using PHP prior to ASP.NET that made me think harder about a problem we’ve all had in ASP.NET.  The basic problem is this:

How do you dynamically change the class of a hyperlink based on the page name so that the hyperlink that represents the current page is styled differently than all of the other hyperlinks on our screen?  If you want you can substitute any other HTML element you want, but the problem remains the same.

The first, most obvious answer would be to create a case statement of some sort.

string fileName = AppRelativeVirtualPath
     case "fileone":
             m_hyperLinkFileOne.CssClass = 
    // etc...

But we all know just how ugly that code will get once we start adding pages to our site.   Yuck!  Unfortunately, this is exactly the code I’ve been recommending up until today.

What was I thinking?!

If you are familiar with ASP.NET at all, you should already be familiar with the Page_Load event handler.  I bet that’s where you do 90% of your page initialization.  But did you know that there is a load event that fires for each control?

Further, you can have all of your controls point to the same load event handler.  So if we take advantage of this, we can write very tight code that automatically sets the class based on the current page name.

// This code assumes that the ID of the hyperLink
// controls follow
// the form of m_hyperLinkDestinationPageName // ie, a link to default.aspx would become
m_hyperLinkDefault void HyperLinkLoad(Object sender, EventArgs args) { // Only need to do this once if you have ViewState // enabled. if(!IsPostBack) { string fileName = AppRelativeVirtualPath .ToLower().Replace("~/","").Replace("/","_") .Replace(".aspx",""); // sender will always point to the control that // fired the event so, assuming that only // asp:hyperlink controls call HyperLinkLoad if("m_hyperlink"+file ==
((HyperLinkControl)sender) .ID.ToLower()) ((HyperLinkControl)sender).CssClass= "selectedClass"; } }


Other post in ASP.NET

Related Post

  • iTextSharp – HTML to PDF – Parsing HTMLiTextSharp – HTML to PDF – Parsing HTML Now that we have the HTML cleaned up, the next thing we will want to do is to parse the HTML. In my actual code for this, I parse the HTML and create the PDF at the same time, but for […]
  • iTextSharp – HTML to PDF – PrerequisitesiTextSharp – HTML to PDF – Prerequisites Before we get into the nitty gritty of parsing the HTML so that we can create PDF code from it, it is important that we develop the concept of how text layout works in iTextSharp.  […]
  • WebForms vs MVC–The War Is OverWebForms vs MVC–The War Is Over loading... I just finished listening to a DotNetRocks podcast today with Paul Sheriff which largely talked about creating mobile web sites using ASP.NET WebForms. During the show they […]
  • iTextSharp – HTML to PDF – Writing the PDFiTextSharp – HTML to PDF – Writing the PDF Last week we parsed the HTML and created code that keeps track of the various attributes we are going to need when we create the PDF.  Today we will finish the code and create the […]
  • ASP.NET Dynamic ValidatorASP.NET Dynamic Validator One of the controls that was added to ASP.NET 3.5 in the SP1 release was the Dynamic Validator control.I completely missed it.What it does is pretty cool.  But it doesn’t […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.

  • Have you given any thoughts to Use Categories to Add Functionality to Classes? I’ve been hard pressed to find good resources, and judging from this article I’m guessing you may have something valuable to say. Thanks in advance!

  • Dave

    Can you point me to any resource that talks about this? I’m not familiar with anything that could be what you reference in your question.