473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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

<li><a href="../themes/" id="n_themes" class="thissection" runat="server"
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
Nov 19 '05 #1
1 3586
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

"Imran Aziz" wrote:
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

<li><a href="../themes/" id="n_themes" class="thissection" runat="server"
>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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Millar | last post by:
wondered if anyone could help me - i'm trying to control the value of an attribute of the BODY tag on a page. So i've got: <html> <body test="colin"> <form ...... ** content ** </form>...
6
by: trinitypete | last post by:
I have a user control that is basically a login dialogue. If I add the user control to webpage1, can I access the properties directly from C# codebehind, or do I have to access the properties from...
0
by: Charles A. Lackman | last post by:
Hello, I am reading a book on ASP.NET Applications and don't understand what the writer is trying to say. It is stated that (when converting a Web Page to a User Control) that: "Adding a...
4
by: Unknown | last post by:
When I create a Table webcontrol programmatically (no static table tag in the aspx file), it renders in the HTML with a border attribute set to "0". If I add my own border...
1
by: Matt | last post by:
If the web control has runat="server" attribute, can we say that control is built on the server (on the fly), and not static control?? Please advise.
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
12
by: Joe via DotNetMonster.com | last post by:
Hi, Within the HTML if I try to put an if statement, I get an error: BC30201: Expression expected This is a simplified example: <%# If StoreNumber = "1" Then %> <b>Store1</b>
6
by: Pete Davis | last post by:
I'm fairly weak with ASP.NET. What I'm trying to do is pass an attribute to a UserControl. The attribute is a value from the CodeBehind for the page. So in my .aspx I have the following: ...
4
by: _Who | last post by:
In the .master file I have: <body runat="server" id="MainBody"> I need to add a background attribute to MainBody in the .master.vb file. But there it tells me that MainBody is not declared. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.