Event handling for dynamically generated controls

  • Thread starter Thread starter Steve Caliendo
  • Start date Start date
S

Steve Caliendo

Hi,

I am dynamically generating check boxes in the Panel control based on a
database field, and I've set the AutoPostBack property to True. How can I
determine which CheckBox caused the post back ?

Thank you,

Steve
 
Steve
Have you wired a handler to the onchange change event of your dynamically generated check box

You should have something like the following prior to adding(or atleast before the response is returned) the checkbox to your panel(In C#)

CheckBox chk = new CheckBox(); //you should already have thi
chk.CheckedChanged += new System.EventHandler(this.chkRuntime_CheckedChanged); //Event handler assignmen
myPanel.Controls.Add(chk); //you should already have thi

Then the handler method

private void chkRuntime_CheckedChanged(object sender, System.EventArgs e

Response.Write("Runtime check clicked")


HTH
Suresh

----- Steve Caliendo wrote: ----

Hi

I am dynamically generating check boxes in the Panel control based on
database field, and I've set the AutoPostBack property to True. How can
determine which CheckBox caused the post back

Thank you

Stev
 
You can do this as follows:
1. For each checkbox set a unique ID (in the ID property of your checkbox
object).
2. Add the same event handler to all the checkbox as follows:
AddHandler newcheck.CheckedChanged, AddressOf check_checked
where newcheck is the name of a checkbox and check_checked is the name of
the method you want the postback event to be posted to.
Apply Steps 1 and 2 for all the checkboxes created.

3. In the check_checked method (the method that handles the event of
checkbox cliecked), read the ID to know which check box was clicked
A small sample for the check_checked method is:
Private Sub check1_checked(ByVal sender As Object, ByVal e As EventArgs)
Response.Write(CType(sender, System.Web.UI.WebControls.CheckBox).ID)
End Sub

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
Back
Top