开发者

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:Bu开发者_开发百科tton 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?


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


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).


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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜