2

I have a custom gridview control that extends the standard asp.net gridview control. The first column of the gridview is composed of dynamically created checkboxfields. I have assigned an event to the CheckChanged event of the checkboxes during the OnRowDataBound event, but the checkboxes do not even fire the event. I have their autopostback property set to true, and they ARE doing a postback, but it doesn't even attempt to fire the OnCheckChanged event. Here is my code: The OnRowDataBound event of the gridview:

protected override void OnRowDataBound(GridViewRowEventArgs e)
        {
            base.OnRowDataBound(e);
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                CheckBox chkSelect = (CheckBox)e.Row.Cells[CheckBoxColumnIndex].FindControl(InputCheckBoxField.CheckBoxID);
                if (chkSelect != null)
                {
                    Guid selectedValue = new Guid(DataKeys[e.Row.RowIndex].Value.ToString());
                    chkSelect.Checked = SelectedValues.Contains(selectedValue);
                    chkSelect.CheckedChanged += new EventHandler(CheckChanged_click);
                }
            }
        }

The CheckChanged event:

protected void CheckChanged_click(object sender, EventArgs e)
        {
            CheckBox chkSelect = (CheckBox)sender;
            GridViewRow gvr = (GridViewRow)chkSelect.Parent.Parent;
            Guid selectedValue = new Guid(DataKeys[gvr.RowIndex].Value.ToString());
            if (chkSelect.Checked && !this.SelectedValues.Contains(selectedValue))
            {
                this.SelectedValues.Add(selectedValue);
            }
            else if (!chkSelect.Checked && this.SelectedValues.Contains(selectedValue))
            {
                this.SelectedValues.Remove(selectedValue);
            }
            DataBind();
        }

One other thing. This USED to work, but as I was developing the control, I discovered that it was databinding multiple times on page load. I went through and began trimming down the databinds so that it would only bind once during page load. This is a side effect of doing that.

I've tried moving the CheckChanged assignment into OnInit, and into OnRowCreated as well, but it still doesn't fire.

1 Answer 1

1

As for your event:

 CheckBox chkSelect = (CheckBox)e.Row.Cells[CheckBoxColumnIndex].FindControl(InputCheckBoxField.CheckBoxID);
      if (chkSelect != null)
      {
           Guid selectedValue = new Guid(DataKeys[e.Row.RowIndex].Value.ToString());
           chkSelect.Checked = SelectedValues.Contains(selectedValue);
           chkSelect.CheckedChanged += new EventHandler(CheckChanged_click);
      }

It looks like you are publishing the event 'after' you change the check state. Try:

 CheckBox chkSelect = (CheckBox)e.Row.Cells[CheckBoxColumnIndex].FindControl(InputCheckBoxField.CheckBoxID);

      if (chkSelect != null)
      {
            chkSelect.CheckedChanged += new EventHandler(CheckChanged_click);
            Guid selectedValue = new Guid(DataKeys[e.Row.RowIndex].Value.ToString());
            chkSelect.Checked = SelectedValues.Contains(selectedValue);
      }

// if your done with chkSelect
chkSelect.CheckedChanged -= CheckChanged_click;

If your event still isn't firing, you will have to step through to see what the SelectedValue.Contains(selectedValue) is returning.

Sign up to request clarification or add additional context in comments.

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.