验证Gridview CheckBox检查多个GridView [英] Validate Gridview CheckBox Checked for Multiple Gridviews
问题描述
我想验证 Gridview
CheckBox
检查了多个 Gridviews
在同一页面上
I want to validate Gridview
CheckBox
Checked for Multiple Gridviews
on the same page
我尝试了以下方法,但无法正常工作。
I have tried the following but it is not working.
<script type="text/javascript">
var TargetBaseControl = null;
window.onload = function () {
try {
//get target base control.
TargetBaseControl = document.getElementById('<%= this.GridView1.ClientID%>', '<%= this.GridView2.ClientID%>');
}
catch (err) {
TargetBaseControl = null;
}
}
function TestCheckBox() {
if (TargetBaseControl == null) return false;
//get target child control.
var TargetChildControl = "chkSelectAdd";
//get all the control of the type INPUT in the base control.
var Inputs = TargetBaseControl.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox' &&
Inputs[n].id.indexOf(TargetChildControl, 0) >= 0 &&
Inputs[n].checked)
return true;
alert('Select at least one checkbox!');
return false;
}
</script>
推荐答案
我可以在这里看到两个问题。 document.getElementById
一次只支持一个元素,所以你的代码只会检查第一个GridView的复选框。此外,一个元素的ID应该是唯一的,所以你在这里检查ID:
There are two problems that I can see here. document.getElementById
only supports one element at a time, so your code will only check the first GridView's checkboxes. Also, an element's ID ought to be unique and so your check against the ID here:
Inputs[n].id.indexOf(TargetChildControl, 0) >= 0 &&
只会在第一个复选框中返回true。
will only return true for the first checkbox.
我建议删除 TargetBaseControl
的用法,而仅仅依靠复选框的名称而不是ID:
I'd suggest removing the use of TargetBaseControl
and simply relying on the names, rather than IDs, of the checkboxes instead:
var Inputs = document.getElementsByTagName("input");
for (var n = 0; n < Inputs.length; ++n)
if (Inputs[n].type == 'checkbox' &&
Inputs[n].name.indexOf(TargetChildControl, 0) >= 0 &&
Inputs[n].checked)
return true;
...
这篇关于验证Gridview CheckBox检查多个GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!