How can I access runat=“server” ASP element using javascript?

前端 未结 4 1586
误落风尘
误落风尘 2021-02-06 03:30

It seems everyone is doing this (in code posts etc.)...but I don\'t know how. :(

Whenever I try to manipulate an asp element using JavaScript I get an "element

4条回答
  •  醉酒成梦
    2021-02-06 04:27

    What's probably happening is that your element/control is within one or more ASP.NET controls which act as naming containers (Master page, ITemplate, Wizard, etc), and that's causing its ID to change.

    You can use "view source" in your browser to confirm that's what's happening in the rendered HTML.

    If your JavaScript is in the ASPX page, the easiest way to temporarily work around that is to use the element's ClientID property. For example, if you had a control named TextBox1 that you wanted to reference via JS:

    var textbox = document.getElementById('<%= TextBox1.ClientID %>');
    

提交回复
热议问题