J
JP
Add Event Handler to dynamic DropDownList
I have a page that contains dynamically generated Dropdown List controls. On
occasion, I want the dynamic dropdown list to perform an AutoPostback. Since
the control is generated programmatically, I bind an event handler to the
control before adding it to the controls collection like sew:
..
..
..
DropDownList controlDropDown = new DropDownList();
controlDropDown.ID = "drop" + drControls["controlName"].ToString();
controlDropDown.AutoPostBack = true;
controlDropDown.SelectedIndexChanged += new
EventHandler(this.controlDropDown_SelectedIndexChanged);
Page.Controls.Add(controlDropDown);
..
..
..
protected void controlDropDown_SelectedIndexChanged(object sender, EventArgs
e)
{
String a = “my answer†//my breakpoint
}
After everything is re-rendered on the post back (Yes I know you have to add
the controls back to the collection on each postback) , the postback occurs
and the controls are re-rendered, but the
controlDropDown_SelectedIndexChanged event does not fire. Why???
I have a page that contains dynamically generated Dropdown List controls. On
occasion, I want the dynamic dropdown list to perform an AutoPostback. Since
the control is generated programmatically, I bind an event handler to the
control before adding it to the controls collection like sew:
..
..
..
DropDownList controlDropDown = new DropDownList();
controlDropDown.ID = "drop" + drControls["controlName"].ToString();
controlDropDown.AutoPostBack = true;
controlDropDown.SelectedIndexChanged += new
EventHandler(this.controlDropDown_SelectedIndexChanged);
Page.Controls.Add(controlDropDown);
..
..
..
protected void controlDropDown_SelectedIndexChanged(object sender, EventArgs
e)
{
String a = “my answer†//my breakpoint
}
After everything is re-rendered on the post back (Yes I know you have to add
the controls back to the collection on each postback) , the postback occurs
and the controls are re-rendered, but the
controlDropDown_SelectedIndexChanged event does not fire. Why???