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

前端 未结 4 1588
误落风尘
误落风尘 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:09

    Making an element runat="server" changes the client-side ID of that element based on what ASP.NET naming containers it's inside of. So if you're using document.getElementById to manipulate the element, you'll need to pass it the new ID generated by .NET. Look into the ClientId property to get that generated ID...you can use it inline in your Javascript like so:

    var element = document.getElementById('<%=myControl.ClientID%>');
    

提交回复
热议问题