I never build our own custom control for web application. So this morning I need to one for Dropdownlist. My new control have one property called SetToZeroIndex, means that if my collection don't have a default value, the DropDwonList will display '--Select--'.It is a very simple control. Here's the step:
- Create new Web Control Library project.
- Inherit DropDownList class.
public class WebComboBox : DropDownList
- Create variable and property SetToZero
private bool setZeroToIndex;
public bool SetZeroToIndex
{
get{ return this.setZeroToIndex; }
set{ this.setZeroToIndex = value; }
}
- override DataBind() to check whether SetToZeroIndex is selected.
public override void DataBind()
{
if(setZeroToIndex==true)
{
base.Items.Add(new ListItem("--Select--","0"));
}
- Add another collection to base class using foreach. Compile and use it. just 10 minute to get my control work and it is reusable. just for my proove-of-concept.