只允许勾选一个复选框 - ASP.NET C#

3

我有一个网格视图,在其中以编程方式添加了复选框。 在 foreach 循环内创建复选框时,可以按照以下操作,使其在选中时触发事件:

            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?

        }

感谢您提前回答。

首先,请告诉我为什么不使用单选按钮而使用复选框?如果您坚持要使用,那么您可以使用jQuery / JavaScript实现相同的功能。 - Viral Jain
@Daredev - 我不知道这是否是原因,但有一个区别:单选按钮无法取消选择(一旦选择了一个),复选框可以。 - Hans Kesting
客户要求使用复选框而不是单选按钮。 - Mana
@user1670729:好的...没问题。那么,JavaScript/jQuery是我们可以考虑的选项吗?使用jQuery可以无缝完成。如果您愿意,我可以提供相关建议。 - Viral Jain
@user1670729: 或者为何不通过使单选按钮看起来像复选框来愚弄用户呢?如果感兴趣,查看这个链接:http://jsfiddle.net/mq8Zq/;https://dev59.com/t3VC5IYBdhLWcg3wfhGL; - Viral Jain
3个回答

3

首先,当这个 CheckBox 被选中时,只有它旁边的其它 CheckBoxes (如果你想要的话)才应该被取消选中,而不是在它被取消选中时。

其次,您可以使用 == 操作符将此复选框与其他复选框进行比较:

CheckBox activeCheckBox = sender as CheckBox;
if(activeCheckBox.Checked)
{
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
        checkBox.Checked = checkBox == activeCheckBox;
    }
}

同样的问题在这里,只有当我向上勾选复选框时才起作用,而向下则不行。这可能是由于foreach循环的性质引起的? - Mana
你在哪里动态创建了复选框?最佳位置是在 RowCreated 中而不是在循环中。我可以问一下为什么你不在 aspx 标记上以模板字段的形式声明添加它们吗? - Tim Schmelter
好的,我已经在页面加载后创建了复选框,而不是在ItemTemplate中绑定GridView。 - Mana
在每次回发(使用相同的ID)中,您必须在page_load最晚的时候重新创建动态控件。否则,事件将无法正确触发且视图状态无法加载。我强烈建议将它们添加到TemplateField中。如上所述,GridView中添加动态控件的唯一其他位置是RowCreated。它在每次回发时被触发(即使GridView没有绑定数据),在页面生命周期的早期阶段,并且对每一行都会触发。 - Tim Schmelter
我真的很想避免使用ItemTemplates,因为这样我将不得不编辑很多代码。 - Mana
显示剩余2条评论

0

我没有尝试过,但这是一种实现的方法:

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?

        }

嗨,这只有在我向上勾选复选框时才有效,向下则无效。这可能是由于foreach循环的性质造成的。有什么解决办法吗? - Mana

0
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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接