3

I have a gridview where i have added checkboxes programmatically. i do as following when creating checkboxes inside a foreach loop, so that they trigger an event when checked,

            cbGV = new CheckBox();
            cbGV.ID = "cbGV";
            cbGV.AutoPostBack = true;
            cbGV.CheckedChanged += new EventHandler(this.cbGV_CheckedChanged);

So basically when i want the event to be triggered, i have the following below,

    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?

        }

thanks for your answer in advance.

5
  • Well, in first place please tell why not use radio-button instead of check-boxes? Still if you insist, then you can achieve the same using jquery/javascript. Commented Nov 7, 2012 at 10:06
  • @Daredev - I don't know if that's the reason here, but there is a difference: radiobuttons can't be unchecked (once one is checked), checkboxes can. Commented Nov 7, 2012 at 10:10
  • the client has requested checkboxes rather then radio buttons. Commented Nov 7, 2012 at 10:17
  • @user1670729: OK... No problem. So, is JavaScript/jQuery an option we can consider? It can be done seamlessly with jQuery. I can provide my inputs for the same if you wish. Commented Nov 7, 2012 at 10:40
  • @user1670729: Or why not fool the user by making radio-button look as check-box? If interested, check this out: jsfiddle.net/mq8Zq; stackoverflow.com/questions/279421/…; Commented Nov 7, 2012 at 10:51

3 Answers 3

3

First, you should only uncheck the other CheckBoxes(if that is what you want) when this CheckBox is checked and not when it was unchecked.

Second, you can use == operator to compare this checkbox with the others:

CheckBox activeCheckBox = sender as CheckBox;
if(activeCheckBox.Checked)
{
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        CheckBox checkBox = ((CheckBox)gvr.FindControl("cbGV"));
        checkBox.Checked = checkBox == activeCheckBox;
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

same problem here, it only works when i'm going through the checkboxes upward, not downwards. this might be due to the nature of the foreach loop?
Where did you create the Checkboxes dynamcially? The perfect place is in RowCreated and not in a loop. Can i ask why you don't add them declaratively in a TemplateField on the aspx markup instead?
btw, could you explaine the following line you wrote, checkBox.Checked = checkBox == activeCheckBox;
That line unchecks all other checkboxes because for only the current checkbox checkBox == activeCheckBox is true(== is a reference comparison). So for all other CheckBoxes false is returned which is assigned to the checkBox.Checked property.
well, i have created the checkboxed under page load, when after binding the gridview. not inside itemtemplate
|
0

I did not try it, but here is one way to do it:

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?

        }

1 Comment

hi, this only works when i'm going through the checkboxes upward, not when downwards. this might be due to the nature of the foreach loop. any idea how to solve this ?
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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.