Mangled IDs for web controls

B

Bill Long

How come sometimes when I define an html tag in an aspx page like

<asp:ListBox id="someID" .../>

it renders like

<select id="_ctl0__ctl0__ctl0_someID" ... />

ListBox=Select ... but the control ID is changed!

How can I turn that off?
 
B

Brock Allen

Because ASP.NET needs a way to uniquely identify the controls and their location
in the server control hierarchy, so it generates a unique "pathed" ID (notice
the ID incorporates parent IDs as well). Why do you care? If you're trying
to use this ID in client side javascript then you'll need get that id by
using the control's ClientID property. You'll then need to incorporate this
into your javascript dynamically.
 
B

Bill Long

Yes I am trying to use the control in client-sde javascript. How can I get
the the ID using the ClientID? Can you give me an example?
 
B

Brock Allen

Here's one way to dynamically incorporate the ClientID into javascript that's
sent back to the browser.

Page_Load()
{
string client_side_id = TextBox1.ClientID;
string js = @"<script>function foo() {{ alert(document.getElementById('{0}').value);
}} </script>";
js = string.Format(js, client_side_id);
Page.RegisterClientScriptBlock("MyJS", js);
}
 
B

Bill Long

Simple enough. I see what I need to do now. Thank you very much for the
assistance.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top