Easyrider
Easyrider

Reputation: 3269

How do I get the correct ID (html) a control in ASP.net?

Everyone knows that the ID for all user controls changes upon the asp.net page is created.

<div id="ID-value" runat="server"></div>

becomes:

<div id="ct100$ID-value"></div>

This could be a pain in the ass I think, especially when you try to bind objects to javascripts. So.

Is there a way to get the correct ID as it is shown in the html page? so that I can manually bind this object to the java script? (in other words: I want the "ct100$.....") Thanks!

Upvotes: 1

Views: 2044

Answers (2)

Yaakov Ellis
Yaakov Ellis

Reputation: 41490

If you are using ASP.net 4.0 or higher, you can try setting the ClientIDMode to be static - this will prevent the Id from being written by ASP.net (but will not give you unique Ids for controls included inside other controls that bind multiple rows).

Upvotes: 3

DotNetUser
DotNetUser

Reputation: 6612

You can do it like below in javascript

document.getElementById('<%=ID-value.ClientID%>')

or in jQuery

$('#<%=(ID-value).ClientID%>')

This is better because you can access elements directly with the actual id's in code-behind.

Upvotes: 5

Related Questions