只允许选中一个复选框-asp.net c#
我有一个gridview,其中我以编程方式添加了复选框。 在foreach循环中创建复选框时,我会执行以下操作,以便在选中复选框时触发事件只允许选中一个复选框-asp.net c#,c#,asp.net,events,gridview,checkbox,C#,Asp.net,Events,Gridview,Checkbox,我有一个gridview,其中我以编程方式添加了复选框。 在foreach循环中创建复选框时,我会执行以下操作,以便在选中复选框时触发事件 cbGV = new CheckBox(); cbGV.ID = "cbGV"; cbGV.AutoPostBack = true; cbGV.CheckedChanged += new EventHandler(this.cbGV_CheckedChanged)
cbGV = new CheckBox();
cbGV.ID = "cbGV";
cbGV.AutoPostBack = true;
cbGV.CheckedChanged += new EventHandler(this.cbGV_CheckedChanged);
因此,基本上,当我想触发事件时,我有以下几点:
protected void cbGV_CheckedChanged(object sender, EventArgs e)
{
//gets the current checked checkbox.
CheckBox activeCheckBox = sender as CheckBox;
foreach (GridViewRow gvr in GridView1.Rows)
{
//this code is for finding the checkboxes in the gridview.
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
//so basically, right here i'm confused on how i should compare the if/else logic, how i should compare and disable every other checkbox if the current checkbox is checked. Any ideas gues?
}
提前感谢您的回答。首先,您应该仅在选中此
复选框时取消选中其他复选框(如果这是您想要的),而不是在取消选中该复选框时取消选中
其次,您可以使用==
运算符将此复选框与其他复选框进行比较:
CheckBox activeCheckBox = sender as CheckBox;
if(activeCheckBox.Checked)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
checkBox.Checked = checkBox == activeCheckBox;
}
}
我没有尝试过,但这里有一种方法:
protected void cbGV_CheckedChanged(object sender, EventArgs e)
{
//gets the current checked checkbox.
CheckBox activeCheckBox = sender as CheckBox;
BOOL flag = false;
CheckBox selectedCheckBox;
foreach (GridViewRow gvr in GridView1.Rows)
{
//this code is for finding the checkboxes in the gridview.
CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
if (checkBox.Checked==true && flag==false)
{
flag = true;
selectedCheckBox = checkBox;
}
else
{
if (checkBox != selectedCheckBox)
{
checkBox.Enabled = false;
checkBox.Checked = false;
}
}
//so basically, right here i'm confused on how i should compare the if/else logic, how i should compare and disable every other checkbox if the current checkbox is checked. Any ideas gues?
}
首先,请告诉我为什么不使用单选按钮而不是复选框?如果您坚持,那么您可以使用jquery/javascript实现同样的效果。@Daredev-我不知道这是否是原因,但有一点不同:单选按钮不能被取消选中(一旦选中),复选框可以。客户端请求的是复选框,而不是单选按钮。@user1670729:确定。。。没问题。那么,JavaScript/jQuery是一个我们可以考虑的选项吗?这可以通过jQuery无缝地完成。如果您愿意,我可以为您提供相同的输入。@user1670729:或者为什么不通过使单选按钮看起来像复选框来愚弄用户?如果感兴趣,请查看以下内容:;嗨,这只在我向上通过复选框时起作用,向下时不起作用。这可能是由于foreach循环的性质造成的。你知道怎么解决这个问题吗?同样的问题,它只在我向上检查复选框时有效,而不是向下。这可能是由于foreach循环的性质造成的?您是在哪里动态创建复选框的?完美的位置是在行中创建的
,而不是在循环中。我可以问你为什么不在aspx标记的TemplateField中声明性地添加它们吗?顺便说一句,你能解释一下你写的以下行吗,checkBox.Checked=checkBox==activeCheckBox;该行取消选中所有其他复选框,因为只有当前复选框checkbox==activeCheckBox
是true
(==是参考比较)。因此,对于所有其他复选框,将返回false
,该复选框分配给复选框。已选中属性。好的,我已在绑定gridview后在页面加载下创建了复选框。不在itemtemplate内
CheckBox activeCheckBox = sender as CheckBox;
// to uncheck all check box
foreach (GridViewRow rw in GrdProc.Rows)
{
CheckBox chkBx = (CheckBox)rw.FindControl("ChkCust");
if (chkBx != activeCheckBox )
{
chkBx.Checked = false;
}
}