简体   繁体   中英

read asp.net control in client side

I'm using the javascript below to read the value of an asp.net control in the client-side. However, it always returns the null value. I'm using similar code at other pages in my website, but now i can't read this specific control. Please suggest anyways I can fix this problem.

<asp:Label ID="srch_data" runat="server" ClientIDMode="Static" ></asp:Label>

var srch_data = document.getElementById("<%= srch_data.ClientID %>");
alert(srch_data);

尝试使用单引号:

var srch_data = document.getElementById('<%= srch_data.ClientID %>').value;

Try this

var srch_data =document.getElementById('srch_data').innerHTML;

ASP.Net Label becomes span after rendering so instead of finding a label which can not be recognized by JS better find

Add the defer attribute to your script element. I tested and it worked. Try something like below -

<%--defer indicates the script to be run after the document is completely parsed.--%>
<script type="text/javascript" language="javascript" defer="true">
        var label = document.getElementById("<%= srch_data.ClientID %>");
        alert("label : " + label);
</script>
<asp:Label ID="srch_data" runat="server" ClientIDMode="Static"></asp:Label>

This should fix your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM