Programatically Declared User Control with Post Back

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a user control that contains a DropDownList and a Submit Button. In the Page_Load method of the User Control, I populate the Drop Down List. When the user clicks on the submit button, I need to retrieve the SelectedValue of the User Control. The code for populating the DropDownList should only run when the page is initialized, so I wrapped the code with if(!IsPostBack)

Everything works as expected when I declaratively add the control to a Page, but when I Programmatically add the control, I lose the ViewState for the DropDownList on the PostBack. Do I need to do something differently to be able to add the user control programmatically vs. declaratively
 
Back
Top