How to add an attribute to a html control in code behind?

I

Imran Aziz

Hello All,

I have a navigation system for the site , in which the links are highlighted
based on which section the site is in. Now the issue is that I do that using
a class="thissection" attribute as show in the below example



THEMES</a></li>



How can I dynamically alter it in code behind, that is add attributes to an
html control using code behind ?

My current code behind does something like this to make the links bold, but
need to add the class attribute to the currently selected section.



Uri inUrl = Request.Url;

String strURL = inUrl.AbsoluteUri;

if (strURL.IndexOf("themes") > 0)

{

n_themes.InnerHtml = "<strong>THEMES</strong>";

n_themes = "";

}

else

{

if (strURL.IndexOf("bookmarks") > 0)

{

n_bookmarks.InnerHtml = "<strong>BOOKMARKS</strong>";

}

else

{

if (strURL.IndexOf("members") > 0)

{

n_members.InnerHtml = "<strong>MEMBERS</strong>";

}

else

{

if (strURL.IndexOf("sources") > 0)

{

n_sources.InnerHtml = "<strong>SOURCES</strong>";

}

else

{

if (strURL.IndexOf("_admin") > 0)

{

n_admin.InnerHtml = "<strong>ADMIN</strong>";

}

else

{

n_home.InnerHtml = "<strong>HOME</strong>";

}

}

}

}

}



Thanks a lot,



Imran
 
G

Guest

If you add the runat="server" attribute to the element you can call the
element on the code behind by its id as an HtmlGenericControl and then set
the attributes like

this.id.Attributes["Attribute"] = value

Hope that helps,

Jason Lind
Senior Software Engineer
Triton Tek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top