Dynamically Populated DropDownList empty on postback

Vote:
 

Hi guys,

Basically what I want to do is in my advanced search I'm wanting to be able to search by section, where the user will be able to specify which section to search using a dropdown list.

If I hard code the listitems the search works fine. However, this isn't ideal. I can initally populate the dropdown with the desired items (if !IsPostBack), however when the page is posted, the list empties and doesn't search within the chosen selection.

I've set EnableViewstate to true in the masterpages, and the page itself, but it makes no difference.

If I have the dropdown populated on pageload, it again searches the root rather than the selected item.

 

Is there anything I'm missing? Its driving me mad!

#31858
Aug 12, 2009 18:25
Vote:
 

This is classic .NET and related to the Page Lifecycle - to fully understand read this article: http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

In short: you need to add your items before ViewState is loaded - use the OnInit method (and you need to add the items also on postbacks).

#31868
Edited, Aug 13, 2009 10:45
Vote:
 

Mari! Got it sorted, can't believe I overlooked this!

 

Much appreciated, tusen takk

#31874
Aug 13, 2009 11:48
Vote:
 

Well I'm 6 years late but today this helped me as well! Tusen takk Mari!

#120253
Apr 14, 2015 9:19
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.