0

Is there a way to add an onCheckedChanged function to a dynamic check box. These checkboxes are being added to a gridview at runtime depending on data in the gridview.

Thanks all

2 Answers 2

1

Yes, you can find control in a row on RowDataBound event of the GridView control and subscribe to the necessary event in codebehind.

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      ((Checkbox)e.Row.FindControl("my_checkbox_id")).CheckedChanged += (sender, args) => {
          // do something in handler
      }

    }

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

Comments

1

Dynamic controls support events only if them are re-created on each postback. So the only one solution is to continuously (on each postback) bind data to the gridview. If you have wrap gridview binding code by !IsPostback condition remove it and check after that.

By the way, be warned that if gridview's datasource data will be changed, the each dynamic control will got a new UniqueID value and those controls events will not be handled.

P.S. maybe the better approach is don't add controls into row dynaically but rather set Visible property value depending on data in DataItem. In this case you may have few server controls in ItemTemplate and show anly one of them.

1 Comment

thanks for the comment Yurly. This made e think more about what i needed to do

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.