2013-02-05 64 views
0

我正在使用下面的javascript读取客户端中的asp.net控件的值。但是,它总是返回空值。我在我网站的其他页面上使用类似的代码,但现在我无法阅读这个特定的控件。请建议反正我可以解决这个问题。在客户端读取asp.net控件

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

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

so srch_data is null?你提到值...但标签没有值 – c0deNinja

回答

0

尝试使用单引号:

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

获取相同的错误“null” – user1951007

+0

当ClientID设置为静态时,不需要使用srch_data.ClientID document.getElementById('srch_data')。innerHTML – Tuscan

0

尝试渲染所以不是发现它不能被JS更好的认识标签后,这个

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

ASP.Net标签变得跨度找到

0

将defer属性添加到脚本元素中。我测试过,它工作。 尝试下面的东西 -

<%--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> 

这应该解决您的问题。

相关问题