BigJim
BigJim

Reputation: 411

how to do a getElementById on an ASP.NET control

I have an element on my page.

<asp:Button ID="buttonToFind" runat="server" OnClick="SomeProcess" />

In javascript I'm trying to find this control using:

document.getElementById("buttonToFind");

However it can't seem to find the control. My understanding is that the asp:Button gets changed to an input element? This input has a new ID that contains the original ID but with a lot of extra characters therefore I can't find the original ID on the page?

Is this correct?

Also given this how would I go about specifying the correct ID to search for?

Upvotes: 3

Views: 6620

Answers (4)

Kiarash
Kiarash

Reputation: 1889

Any server control will be gave new client side Id so yuo can use the ClientId of any control you wana pass to Javascript :) so

document.getElementById('<%= buttonToFind.ClientID %>'); 

should be your answer

Upvotes: 0

jordanbtucker
jordanbtucker

Reputation: 6078

You need the ClientID property of the control. In ASP.NET 4 you can also set the ClientIDMode to Static. Source

Upvotes: 3

jball
jball

Reputation: 25014

You need to get the server control's client id:

document.getElementById("<%=buttonToFind.ClientID%>");

Upvotes: 0

RPM1984
RPM1984

Reputation: 73112

Your understanding is correct.

This will work if the JS is on the ASPX/ASCX markup:

document.getElementById('<%= buttonToFind.ClientID %>');

If the JS is external, you'll need to do extra work (e.g use a literal to hold the ID's, or register a script).

Upvotes: 1

Related Questions