473,503 Members | 479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically changing the ID of a control

I've got a c# app, and in the HTML in front I have a <divtag around
some navigation controls, and I want to dynamically change it's ID so
the CSS kicks in and highlights the currently selected option.

Problem is - when you set an ID, it prefixes it with ctl0_ on IIS 5.1
(Windows XP - our dev machines) and ctl00_ on IIS 6.0 (Windows 2003
server - the production machine)

The obvious answer is to create two versions of style stylesheet - one
for dev, one for production. The problem with this is trying to
explain to management why we'd need to modify the CSS outside of
source control every time we deploy it; in short, it's just not going
to happen.

Does anyone know how to override the .net prefix (ctl..), so I can set
the ID of an element with a consistent value?

Code is:

---/ snip /---

<div id="myDiv" runat="server">
<ul>
<li><a href="default.aspx">Home</a></li>
<li><a href="Item1.aspx">Item 1</a></li>
</ul>
</div>

---/ snip /---

string scriptName =
Request.ServerVariables["SCRIPT_NAME"].ToLower(); // get current
script name
string newID = scriptName.SubString(0, scriptname.IndexOf(".")); //
remove extension - home or item1

myDiv.ID = newID; // sets it to ctl0_home or ctl00_home, ctl0_item1 or
ctl00_item1

TIA

Mar 21 '07 #1
4 5891
Hi,

"Dunc" <du**********@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
I've got a c# app, and in the HTML in front I have a <divtag around
some navigation controls, and I want to dynamically change it's ID so
the CSS kicks in and highlights the currently selected option.
I'm not sure what the control's ID has to do with the CSS being used. The
Css of a controls is set using CssClass, not ID
Problem is - when you set an ID, it prefixes it with ctl0_ on IIS 5.1
(Windows XP - our dev machines) and ctl00_ on IIS 6.0 (Windows 2003
server - the production machine)
The obvious answer is to create two versions of style stylesheet - one
for dev, one for production. The problem with this is trying to
explain to management why we'd need to modify the CSS outside of
source control every time we deploy it; in short, it's just not going
to happen.
That is not a good solution , what happen if after an upgrade (of either the
framework or IIS ) the controls are named differentely?
Mar 21 '07 #2
You're right - it's not a great idea, but it's the only one I can
think of. The reason I need an ID tag as opposed to just setting a
CssClass is the element also needs to be referenced by some
JavaScript.

Thanks for the feedback, though - got any other thoughts?

Dunc

---/ snip /---

On Mar 21, 2:46 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
Hi,

"Dunc" <duncan.we...@gmail.comwrote in message

news:11**********************@n59g2000hsh.googlegr oups.com...
I've got a c# app, and in the HTML in front I have a <divtag around
some navigation controls, and I want to dynamically change it's ID so
the CSS kicks in and highlights the currently selected option.

I'm not sure what the control's ID has to do with the CSS being used. The
Css of a controls is set using CssClass, not ID
Problem is - when you set an ID, it prefixes it with ctl0_ on IIS 5.1
(Windows XP - our dev machines) and ctl00_ on IIS 6.0 (Windows 2003
server - the production machine)
The obvious answer is to create two versions of style stylesheet - one
for dev, one for production. The problem with this is trying to
explain to management why we'd need to modify the CSS outside of
source control every time we deploy it; in short, it's just not going
to happen.

That is not a good solution , what happen if after an upgrade (of either the
framework or IIS ) the controls are named differentely?

Mar 21 '07 #3
Instead of monkeying around with trying to change the control's mangled id,
why not just change the css class attribute? You can do this server side, or
you can even do it client side via javascript.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Dunc" wrote:
I've got a c# app, and in the HTML in front I have a <divtag around
some navigation controls, and I want to dynamically change it's ID so
the CSS kicks in and highlights the currently selected option.

Problem is - when you set an ID, it prefixes it with ctl0_ on IIS 5.1
(Windows XP - our dev machines) and ctl00_ on IIS 6.0 (Windows 2003
server - the production machine)

The obvious answer is to create two versions of style stylesheet - one
for dev, one for production. The problem with this is trying to
explain to management why we'd need to modify the CSS outside of
source control every time we deploy it; in short, it's just not going
to happen.

Does anyone know how to override the .net prefix (ctl..), so I can set
the ID of an element with a consistent value?

Code is:

---/ snip /---

<div id="myDiv" runat="server">
<ul>
<li><a href="default.aspx">Home</a></li>
<li><a href="Item1.aspx">Item 1</a></li>
</ul>
</div>

---/ snip /---

string scriptName =
Request.ServerVariables["SCRIPT_NAME"].ToLower(); // get current
script name
string newID = scriptName.SubString(0, scriptname.IndexOf(".")); //
remove extension - home or item1

myDiv.ID = newID; // sets it to ctl0_home or ctl00_home, ctl0_item1 or
ctl00_item1

TIA

Mar 21 '07 #4
Hi,

"Dunc" <du**********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
You're right - it's not a great idea, but it's the only one I can
think of. The reason I need an ID tag as opposed to just setting a
CssClass is the element also needs to be referenced by some
JavaScript.
But that is different, it's not the same knowing in the client the ID of a
given control than wanting it to change dynamically.

You can use a variable to hold the assigned clientID of the control, y ou
could do something like:

Controls.Add( new LiteralControl( "<scriptvar myControl = '" +
yourControl.ClientID + "'; </script>:" );

now in the client you can reference it like:

<script>
function DoIt()
{
var control = document.getElementByID( myControl);
control.class = "theCSSClass";
}
</script>
Mar 21 '07 #5

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

Similar topics

2
1538
by: ilPostino | last post by:
ok, so I have my asp.net application. I want a single page which can load asp user controls and at runtime. How can this be done? I don't want to drag the control onto the page because then I...
7
3247
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
1
8135
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
4
3774
by: Chris Mahoney | last post by:
Hi Currently I am setting the background image of my page by using the following code: <style type="text/css"> BODY { BACKGROUND-IMAGE: url(myimage.jpg) } </style> What I would like to do...
6
2237
by: Test Message | last post by:
I need to dynamically add an html control to a web form. What I have, thus far, adds the control to the page, but only places it after the closing "</html>" tag. ...
1
1022
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
5
9735
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....
2
1171
by: tshad | last post by:
Is there a way to dynamically add in User Controls where I would get the name from a session variable: If I have the following code:...
6
11057
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
7203
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
7282
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
7463
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
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.