views:

878

answers:

1

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.

A: 

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.

dboarman