Reputation: 641
I have a custom server control inheriting from DropDownList. On postback, the items are lost. It looks something like this:
public class MyClientSelectList : DropDownList
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// design mode hack to let visual studio display in design mode
if (!DesignMode)
{
if (!Page.IsPostBack)
{
this.Items.Add(new ListItem("Select an item..."));
// add more items from db
}
}
}
}
I checked EnablePostBack = true. I select a selected value in the page load of my page which is hosting this custom server control.
Why are the items lost on postback?
Upvotes: 0
Views: 1123
Reputation: 1417
EnableViewState is already True by default, so mshsayem's solution will not work.
There're 2 way to do that, a standard way is to override SaveControlState and LoadControlState Method
Refer to http://msdn.microsoft.com/en-us/library/1whwt1k7(v=VS.100).aspx
Another way is, in the Init, reload list items from database no matter postback or not, then retrieve the selected value from post data and set it back to dropdownlist
Upvotes: 1