Introduction: In
this article today I will explain how we can fill dropdownlist with days, month
and year in asp.net
Description:
In the previous article I have explained Server side validation in MVC Razor using data annotation,Like linq query example and bind gridview,Create a dynamic Slider in asp.net, URL routing in asp.net website, Populate Dropdown List dynamically using Asp.net MVC Razor, Code First migration in asp.net MVC 4, Populate Cascading Dropdown List in Asp.net MVC4 using Json and Jquery and What is Asp.net MVC? Its advantages and disadvantges.
Add a webform to application and design the .aspx page as mention below:
<fieldset style="width:400px">
<legend>Fill Dropdownlist with Year,Month and Date
Example</legend>
Year: <asp:DropDownList ID="ddlyear"
runat="server"
AutoPostBack="True"
onselectedindexchanged="ddlyear_SelectedIndexChanged"
></asp:DropDownList>
Month: <asp:DropDownList ID="ddlmonth"
runat="server"
AutoPostBack="True"
onselectedindexchanged="ddlmonth_SelectedIndexChanged">
</asp:DropDownList>
Day: <asp:DropDownList ID="ddlday" runat="server">
</asp:DropDownList>
</fieldset>
Now write the code on .aspx.cs
file:
using System.Globalization;
protected void Page_Load(object sender, EventArgs
e)
{
if (!Page.IsPostBack)
{
for (int
i = 1980; i <= 2015; i++)
{
ddlyear.Items.Add(i.ToString());
}
ddlyear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected
= true;
Fillmonth();
FillDays();
}
}
private void
FillDays()
{
ddlday.Items.Clear();
int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlyear.SelectedValue), Convert.ToInt32(ddlmonth.SelectedValue));
for (int i = 1; i
<= noofdays; i++)
{
ddlday.Items.Add(i.ToString());
}
ddlday.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected
= true;
}
private void
Fillmonth()
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
for (int i = 1; i
< 13; i++)
{
ddlmonth.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
ddlmonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected
= true;
}
protected void
ddlyear_SelectedIndexChanged(object sender, EventArgs e)
{
FillDays();
}
protected void
ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
{
FillDays();
}
In VB:
Imports System.Globalization
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
For i As Integer = 1980 To 2015
ddlyear.Items.Add(i.ToString())
Next
ddlyear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected
= True
Fillmonth()
FillDays()
End If
End Sub
Private Sub FillDays()
ddlday.Items.Clear()
Dim noofdays As Integer = DateTime.DaysInMonth(Convert.ToInt32(ddlyear.SelectedValue), Convert.ToInt32(ddlmonth.SelectedValue))
For i As Integer = 1 To noofdays
ddlday.Items.Add(i.ToString())
Next
ddlday.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected
= True
End Sub
Private Sub Fillmonth()
Dim info As DateTimeFormatInfo = DateTimeFormatInfo.GetInstance(Nothing)
For i As Integer = 1 To 12
ddlmonth.Items.Add(New ListItem(info.GetMonthName(i),
i.ToString()))
Next
ddlmonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected
= True
End Sub
Protected Sub ddlyear_SelectedIndexChanged(sender As Object, e As EventArgs)
FillDays()
End Sub
Protected Sub ddlmonth_SelectedIndexChanged(sender As Object, e As EventArgs)
FillDays()
End Sub
Run the project and check the result.
No comments:
Post a Comment