Bind Date and Year Drop down list in asp.net
The following code is show how to bind the drop down list box from server-side function in asp.net.Create dropdown List box in asp.net
The following code is used to create two label and two drop down list box.
<asp:Label ID="Label2" runat="server" Text="Select Year"></asp:Label>
<asp:DropDownList ID="ddlyear" runat="server">
</asp:DropDownList>
<asp:Label ID="Label3" runat="server" Text="Select Month"></asp:Label>
<asp:DropDownList ID="ddlmonth" runat="server">
</asp:DropDownList>
You should include the following namespace.
using System.Globalization;
Defines how System.DateTime values are formatted and displayed, depending on the culture.Bind Year and Month in Dropdown list box
Two function are defined in the page load and every function bind the month and year in the drop down list box.
protected void Page_Load(object sender, EventArgs e)
{
bind_month_ddl();
bind_year_ddl();
}
private void bind_year_ddl()
{
int year = (System.DateTime.Now.Year);
for (int intCount = year; intCount >= 1980; intCount--)
{
ddlyear.Items.Add(intCount.ToString());
}
}
private void bind_month_ddl()
{
for (int i = 1; i <= 12; i++)
{
ddlmonth.Items.Add(new System.Web.UI.WebControls.ListItem(DateTimeFormatInfo.CurrentInfo.GetMonthName(i), i.ToString()));
}
}
Output :
NICE!
ReplyDeleteThank you
Delete