Extending a DropDownList control
- by Andrew Robinson
I have a rather large application that has literally a hundred DDLs with Yes / No ListItems. In an attempt to same 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"));
}
}
}