Microsoft JScript 运行时错误: 'undefined' 为空或不是对象 我的CheckBox是在GridView中

代码:<asp:CheckBox ID="CheckBox1" name="CheckBox1" runat="server" value= '<%# DataBinder.Eval(Container.DataItem,"id") %>' onclick="checkJs()"/>
下面是javascript的代码:
<script type="text/javascript">
var _selchk = document.getElementById("selchk");
function checkJs() {
var tempstr = "";
var chklist = document.form1.ChexkBox1;
for (var i = 0; i <= document.form1.ChexkBox1.length - 1; i++) {
if (document.form1.ChexkBox1.checked == true) {
tempstr = tempstr + chklist[i].value + ",";
}
}
_selchk.value = tempstr;
}
</script>

原理是在服务器端把服务器控件的客户端id以js的形式输出即可。比如你有个服务器端控件的id是test1,那你就在aspx中写
<script>
var myControllerId = <%=test1.ClientId%>
</script>
然后在其他js中用document.getElementById(myControllerId)即可获得该服务器控件。

如果你需要应用到js调用控件,我建议最好还是用Html控件吧,把runat=“server”加上和服务器控件一样可以后台调用。能给我点分不?
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-27
绑在GridView中的服务器控件ID无效

在客户端按HTML 树型结构获取
相似回答
大家正在搜