[英]Javascript validation for a dynamic gridview have checkbox
I have created a GridView Dynamically. 我创建了一个动态的GridView。 This dynamic gridview has two Template Field column. 此动态gridview具有两个“模板字段”列。 Both the column has checkboxes in every row. 这两列的每一行都有复选框。 How do I write the client side validation so that only one checkbox is checked at a given time in a particular row. 我怎样写的客户端验证,以便只有一个复选框是在特定行在给定时间检查。
I tried using this code but it didn't work 我试图用这个代码,但没有奏效
function MutExcCheckBox()
{
var wipChk = document.getElementById("ckhBoxSelect");
var aukChk = document.getElementById("chkBoxABC");
for(i = 0; i<wipChk.length ; i++)
{
if(wipChk[0].checked==true)
{
aukChk[0].checked=false;
}else if (aukChk[0].checked==true)
{
wipChk[0].checked=false;
}
}
I have added the Onclick
attribute from code behind. 我从后面的代码中添加了Onclick
属性。
ckh.Attributes.Add("Onclick", "MutExcCheckBox()");
Thanks 谢谢
First of all go to jQuery site and download latest version of jQuery. 首先,转到jQuery网站并下载最新版本的jQuery。 Include this in your page. 将此包含在您的页面中。
EDIT:- I'm very sorry it the first solution didn't work for you. 编辑:-非常抱歉,第一个解决方案对您不起作用。 Actually when GridView and controls contained in it are rendered, they are wrapped in side don't know what :) Here is aspx code: 实际上,当渲染GridView及其包含的控件时,它们被包装在一边不知道是什么:)这是aspx代码:
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Checkbox 1">
<ItemTemplate>
<asp:CheckBox ID="chkFrist" CssClass="chk1" runat="server"
Text='<%# Eval("Chk1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Checkbox 2">
<ItemTemplate>
<asp:CheckBox ID="chkSecond" CssClass="chk2" runat="server"
Text='<%# Eval("Chk2") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
JS (jQuery), you can add this anywhere before body tag or inside head. JS(jQuery),您可以在body标签之前或head内部添加此内容。 I prefer to add scripts just above closing body tag and inclusion of external scripts inside head tag. 我更喜欢在封闭体标签上方添加脚本,并在head标签内包含外部脚本。
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script src="jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".chk1 :checkbox, .chk2 :checkbox").click(function() {
checkGrid(this);
});
});
function checkGrid(elem) {
var chkd = $(elem).attr("checked");
//WHEN YOU SPECIFY CSSCLASS ON CHECKBOX IT IS WRAPPED
//INSIDE A SPAN ELEMENT WITH GIVEN CLASS
var cls = $(elem).parent().attr("class");
if (chkd) {
if (cls == "chk1")
$(elem).parents("tr").find(".chk2 :checkbox")
.attr('checked', !chkd);
else
$(elem).parents("tr").find(".chk1 :checkbox")
.attr('checked', !chkd);
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.