DropDownList empty after postback

Getting an empty DropDownList after postback and although you selected a value, DropDownList.SelectedIndex is -1.

This is most likely the case of the viewstate being turned off. If you manually populate the list, there's a workaround without using the viewstate. This is accomplished by understanding the event order.

Simplified it works as follows:
  • Init - page is initiliazed
  • ProcessPostData - handles the data posted to the page
  • Load - page is loaded and ready to access

If your DropDownList doesn't contain any values when ProcessPostData is reached it will ignore the posted values while recreating your DropDownList-control.

The trick is to always populate your DropDownList at Page_Init like:
protected void Page_Init(object sender, EventArgs e) {

Now you will have be able to read the selected in your Page_Load or event handler!

Related posts:


comments powered by Disqus