I have a rather large application that has literally a hundred DDLs with Yes / No ListItems. In an attempt to save myself some time, I created a custom control that extends the standard DDL.
It all seems to work fine but I am having some issues when assigning the SelectedValue property in code where the selected value does not seem to have an affect on the control. I wonder if I should be adding my items during Init or PagePreLoad? Should I be calling base.OnInit before or after I add the list items? This mostly works but not 100%. (v3.5)
public class YesNoDropDownList : DropDownList
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsPostBack)
{
base.Items.Add(new ListItem("Yes", "YES"));
base.Items.Add(new ListItem("No", "NO"));
}
}
}
I think the issue is that if I load ListItems in Init, that is before viewstate is established and the ListItems are lost on postback. If I load them in OnLoad, that is after SelectedValue is applied and if I am setting SelectedValue, the selection is lost. My solution was wire up the Page InitComplete event in the OnInit override. This works but I am not sure that it is the best solution.
So, either Page_InitComplete as detailed below or OnInit but I have to load the items every time. Thoughts?
protected override void OnInit(EventArgs e)
{
this.Page.InitComplete += new EventHandler(Page_InitComplete);
base.OnInit(e);
}
private void Page_InitComplete(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
base.Items.Add(new ListItem("Yes", "YES"));
base.Items.Add(new ListItem("No", "NO"));
}
}