Explanation:We are going to explain it through a small example. Here we are creating a "DropDownList" which will be filled with years data.
The simple DropDownList look like this:
<asp:DropDownList ID="DropDownListYear" runat="server">asp:DropDownList>
Now, we want to add a reqiuerd Field Validator. So User have to select any field from years. Here is the Reqiuerd Filed validator code.
:RequiredFieldValidator ID="RequiredFieldValidator3" Display="Dynamic" runat="server" InitialValue="[Year]" ControlToValidate="DropDownListYear" rrorMessage="Select Year">*asp:RequiredFieldValidator>
After that we have fixed the sketch of our DropDownlist. We are going Populate the DropDownList with its Data. Following is the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int currYear = DateTime.Now.Year - 10;
int[] years = new int[91];
for (int i = currYear; i > currYear - 91; i--)
{
years[currYear - i] = i;
}
DropDownListYear.DataSource = years;
DropDownListYear.DataBind();
ListItem listitm = new ListItem("[Year]");
listitm.Selected = true; //make it DropDownList default item
DropDownListYear.Items.Insert(0, listitm);
}
}
|