【问题标题】:allow only one checkbox to be checked - asp.net c#只允许选中一个复选框 - asp.net c#
【发布时间】:2012-10-27 08:47:23
【问题描述】:

我有一个gridview,我在其中以编程方式添加了复选框。 在 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 实现同样的效果。
  • @Daredev - 我不知道这是否是原因,但有区别:单选按钮不能取消选中(一旦选中),复选框可以。
  • 客户端请求的是复选框而不是单选按钮。
  • @user1670729:好的……没问题。那么,我们可以考虑使用 JavaScript/jQuery 吗?它可以用 jQuery 无缝完成。如果您愿意,我可以提供我的意见。
  • @user1670729:或者为什么不通过让单选按钮看起来像复选框来欺骗用户?如果有兴趣,请查看:jsfiddle.net/mq8Zqstackoverflow.com/questions/279421/…;

标签: c# asp.net events gridview checkbox


【解决方案1】:

首先,您应该仅在选中此 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 循环的性质造成的?
  • 您在哪里动态创建了复选框?完美的地方是RowCreated,而不是循环。我能问一下你为什么不在 aspx 标记上的 TemplateField 中以声明方式添加它们吗?
  • 顺便说一句,你能解释一下你写的下面这行吗,checkBox.Checked = checkBox == activeCheckBox;
  • 该行取消选中所有其他复选框,因为只有当前复选框 checkBox == activeCheckBoxtrue(== 是参考比较)。因此,对于所有其他 CheckBoxes,false 被返回,它被分配给 checkBox.Checked 属性。
  • 好吧,我在页面加载下创建了复选框,在绑定gridview之后。不在项目模板内
【解决方案2】:

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

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 循环的性质。知道如何解决这个问题吗?
【解决方案3】:
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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多