473,394 Members | 1,748 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,394 software developers and data experts.

Dynamically change tag attribute

Hi there,

I have a CSS based menu in a master page that uses class attributes to
determine which item is currently selected. So, for example if the Homepage
is currently the active page then its list item tag carries a class
attribute of "current", while all other items carry one of "select".

What I want to know is what is the best approach to change these attributes
based on the current content page. Should I be changing the attribute in
the pre-render event using HTMLTextWriter or would some kind of client-side
script work better here?

All advice is welcome! What's best practice?

Thanks

John
Dec 14 '07 #1
7 2102
"John" <Jo*******@community.nospamwrote in message
news:Oo**************@TK2MSFTNGP04.phx.gbl...
What I want to know is what is the best approach to change these
attributes based on the current content page. Should I be changing the
attribute in the pre-render event using HTMLTextWriter or would some kind
of client-side script work better here?
No need either for HTMLTextWriter or client-side script.

1) Set all of the menu item attributes to 'select' by default

2) Declare a property in the MasterPage's class

3) When the content page loads, make it set the value of the MasterPage
variable

4) When the MasterPage loads, interrogate the value of the MasterPage
variable and set the appropriate menu item attribute to 'current'

http://aspnet.4guysfromrolla.com/articles/013107-1.aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 14 '07 #2
Perfect. Many thanks Mark, I was hoping there would be an easier solution
like this.

Best regards

John
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:us**************@TK2MSFTNGP05.phx.gbl...
"John" <Jo*******@community.nospamwrote in message
news:Oo**************@TK2MSFTNGP04.phx.gbl...
>What I want to know is what is the best approach to change these
attributes based on the current content page. Should I be changing the
attribute in the pre-render event using HTMLTextWriter or would some kind
of client-side script work better here?

No need either for HTMLTextWriter or client-side script.

1) Set all of the menu item attributes to 'select' by default

2) Declare a property in the MasterPage's class

3) When the content page loads, make it set the value of the MasterPage
variable

4) When the MasterPage loads, interrogate the value of the MasterPage
variable and set the appropriate menu item attribute to 'current'

http://aspnet.4guysfromrolla.com/articles/013107-1.aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 14 '07 #3
Hello Mark,

I've got the property declared in the MasterPage class, but can you give me
hint on how to set the various <ultags' attributes. I'm giving them
unique IDs so can I reference them directly in some way or do I have to walk
through the DOM checking for each one (I'm guessing not)?

Thanks

John

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:us**************@TK2MSFTNGP05.phx.gbl...
"John" <Jo*******@community.nospamwrote in message
news:Oo**************@TK2MSFTNGP04.phx.gbl...
>What I want to know is what is the best approach to change these
attributes based on the current content page. Should I be changing the
attribute in the pre-render event using HTMLTextWriter or would some kind
of client-side script work better here?

No need either for HTMLTextWriter or client-side script.

1) Set all of the menu item attributes to 'select' by default

2) Declare a property in the MasterPage's class

3) When the content page loads, make it set the value of the MasterPage
variable

4) When the MasterPage loads, interrogate the value of the MasterPage
variable and set the appropriate menu item attribute to 'current'

http://aspnet.4guysfromrolla.com/articles/013107-1.aspx
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 17 '07 #4
"John" <Jo*******@community.nospamwrote in message
news:Ob**************@TK2MSFTNGP03.phx.gbl...
>>What I want to know is what is the best approach to change these
attributes based on the current content page. Should I be changing the
attribute in the pre-render event using HTMLTextWriter or would some
kind of client-side script work better here?

No need either for HTMLTextWriter or client-side script.

1) Set all of the menu item attributes to 'select' by default

2) Declare a property in the MasterPage's class

3) When the content page loads, make it set the value of the MasterPage
variable

4) When the MasterPage loads, interrogate the value of the MasterPage
variable and set the appropriate menu item attribute to 'current'

http://aspnet.4guysfromrolla.com/articles/013107-1.aspx

I've got the property declared in the MasterPage class, but can you give
me hint on how to set the various <ultags' attributes. I'm giving them
unique IDs so can I reference them directly in some way or do I have to
walk through the DOM checking for each one (I'm guessing not)?
This is explained in the 4Guys article.

Alternatively, you can use FindControl which achieves the same effect,
though purists would no doubt say that it's not very OOP... ;-)

E.g. to set the text in a TextBox control on the MasterPage from the content
page, you could do something like:

((TextBox)Master.FindControl("MyTextBox")).Text = "Hello";
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 17 '07 #5
Hi Mark,

Thanks for this. I did read the article, which was very useful, but from my
first reading this only applied to server controls (also the FindControl
method?).

Does this also work for plain old HTML tags as well?

Apologies if I'm being slow on this......

Best regards

John

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:uK**************@TK2MSFTNGP06.phx.gbl...
"John" <Jo*******@community.nospamwrote in message
news:Ob**************@TK2MSFTNGP03.phx.gbl...
>>>What I want to know is what is the best approach to change these
attributes based on the current content page. Should I be changing the
attribute in the pre-render event using HTMLTextWriter or would some
kind of client-side script work better here?

No need either for HTMLTextWriter or client-side script.

1) Set all of the menu item attributes to 'select' by default

2) Declare a property in the MasterPage's class

3) When the content page loads, make it set the value of the MasterPage
variable

4) When the MasterPage loads, interrogate the value of the MasterPage
variable and set the appropriate menu item attribute to 'current'

http://aspnet.4guysfromrolla.com/articles/013107-1.aspx

I've got the property declared in the MasterPage class, but can you give
me hint on how to set the various <ultags' attributes. I'm giving them
unique IDs so can I reference them directly in some way or do I have to
walk through the DOM checking for each one (I'm guessing not)?

This is explained in the 4Guys article.

Alternatively, you can use FindControl which achieves the same effect,
though purists would no doubt say that it's not very OOP... ;-)

E.g. to set the text in a TextBox control on the MasterPage from the
content page, you could do something like:

((TextBox)Master.FindControl("MyTextBox")).Text = "Hello";
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 17 '07 #6
"John" <Jo*******@community.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>((TextBox)Master.FindControl("MyTextBox")).Text = "Hello";

Thanks for this. I did read the article, which was very useful, but from
my first reading this only applied to server controls (also the
FindControl method?).

Does this also work for plain old HTML tags as well?
Yep - just give them an ID and mark them runat="server"
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 17 '07 #7
Great. Thanks very much for your help Mark.

best regards

John

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:uU**************@TK2MSFTNGP06.phx.gbl...
"John" <Jo*******@community.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>((TextBox)Master.FindControl("MyTextBox")).Tex t = "Hello";

Thanks for this. I did read the article, which was very useful, but from
my first reading this only applied to server controls (also the
FindControl method?).

Does this also work for plain old HTML tags as well?

Yep - just give them an ID and mark them runat="server"
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Dec 17 '07 #8

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

Similar topics

9
by: pablo | last post by:
Dear NGers, I would like to change the alt-text with the changing of the image during a mouseover action. Can document.images.altView be changed dynamically? TIA, pablo
27
by: Nicholas Couch | last post by:
I have a little form with a couple of dynamically generated list boxes. When the user makes a selection from the first box, the second box is refreshed. When they make a selection from the second...
6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
5
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator;...
5
by: Angel | last post by:
Is there a way to create an IFRAME dynamically via VB.NET. In other words creating the HTML element in the server side code? thanks in advance....
4
by: Joe | last post by:
I have an xml file which looks similar to this: <data> Please enter the value: <dynamicControl id="myDC", type="Textbox", MaxLength="100"/> </data> I am trying to devise a way to have a web...
3
by: ted benedict | last post by:
hi everybody, didn't find this using the search :( this is my problem: i create a dom element dynamically (<span>) and want to assign a class attribute to it such that it has some css style, this...
6
by: mayurkoul2002 | last post by:
hello there..... i have a problem here...........i want to assign ID attribute dynamically to the html tags......and i need a function for tht....can anybody help me out in tht
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.