1

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?

3
  • 1
    Good read: msdn.microsoft.com/en-us/library/ms178472.aspx Commented Jun 4, 2012 at 21:37
  • @Mike Christensen - you beat me to that link by a few seconds. ifyou want to post an answer, I'll delete mine since you were first. Commented Jun 4, 2012 at 21:39
  • @DavidStratton - Heh, ok. I was hoping someone would write a more detailed answer (I haven't done any ASP.NET dev in a few years now so it's all a bit hazy to me, I just remember this stuff was kinda confusing).. I say keep your answer though, it's certainly valuable. Commented Jun 4, 2012 at 21:48

1 Answer 1

2

The short answer is that by the time ChooseSODropDown has 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.

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

Comments

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.