My setup is as follows:
In the PageLoad event, I initialize a dropDownList as follows:
SONumList = new DropDownList();
SONumList.DataSource = SOList;
SONumList.DataBind();
SONumList.Height = new Unit("19px");
SONumList.SelectedIndexChanged += (ChooseSODropDown);
SONumList.AutoPostBack = true;
Panel1.Controls.Add(SONumList);
In the ChooseSODropDown event that fires when the SelectedIndex on SONumList is changed, I create another DropDownList called PNum:
PNumList = new DropDownList();
PNumList.DataSource = dataSource2;
PNumList.DataTextField = "Part";
PNumList.DataValueField = "Part";
PNumList.DataBind();
PNumList.Height = new Unit("19px");
PNumList.SelectedIndexChanged += ChoosePNumDropDown;
PNumList.AutoPostBack = true;
Panel1.Controls.Add(PNumList);
Although the PNum box itself displays properly and has the data appropriately bound, the ChoosePNumDropDown event never fires, even though the page does postback. I’ve tried a breakpoint at the beginning of the function and it doesn’t fire at all.
Is there some reason why I would be unable to bind events to an object inside of another event firing?
The short answer is that by the time
ChooseSODropDownhas been fired, it’s too late to bind any more event handlers to existing controls. This is because the previous state of a control is deserialized from the viewstate within OnLoad, and that initial state is used to trigger an onchange event to be fired.I believe you’ll have to initialize all controls in OnLoad, including the event handler for
ChoosePNumDropDown, and move any required logic into that event handler. To my knowledge, there is no way to add more event handlers within an existing event handler and expect it to be fired in the same postback.I’d recommend reading up in the Page Lifecycle to get acquainted with exactly how events are processed.