omega
omega

Reputation: 43833

How to get an asp control id from c# code?

I have a asp control button that when I look at it in IE 9 developers tool I see it's id is ctl00_m_g_0e632166_55b3_4470_971c_bd562d4904c8_ctl00_Button2.

Is there a way I can access this id in the c# code? Something like Button2.getID?

Curently I am inserting javascript code that will need to find this control to do various things and the method I am using isn't good, it's basically checking if the suffix of all input id's is Button2. I want to find a better way...

Thanks

Upvotes: 0

Views: 783

Answers (4)

Developer
Developer

Reputation: 3057

alert($("#<%= Button2.ClientID %>").click());

Upvotes: 0

Roland Mai
Roland Mai

Reputation: 31077

If your control is not in a repater or grid, and it is unique to the page, you can set ClientIDMode="Static" and it will be whatever ID you give it. Otherwise, you will have to use ClientID as others have pointed out.

Upvotes: 2

Constantin Baciu
Constantin Baciu

Reputation: 234

Control.ClientID should get you the DOM element ID.

Upvotes: 2

Jamiec
Jamiec

Reputation: 136074

Button2.ClientID is what you're looking for i think.

docs: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

You can also control how this is set by setting the ClientIDMode

Upvotes: 4

Related Questions