开发者

Programmatically assigned CheckChanged event does not fire for checkbox control in gridview

开发者 https://www.devze.com 2022-12-19 09:27 出处:网络
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 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 eve开发者_如何转开发nt. 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号