Introduction: In previous articles i explained How to fill DropDownList from Sql server database in asp.net and how to Fill CheckBoxList based on DropDownList selection in asp.net(C#, VB) and How to open Pop up window on Drop down selection in Asp.net. Now in this article I am going to
explain one of the common requirements while creating user input form like
registration form where user has to select his date of birth, joining date etc.
Description: So here in this example I have demonstrated with example How to fill/ load/ bind years, months and days in the DropDownList . Current date will be selected in the DropDownList by default and when user select another month from the DropDownList then corresponding number of days will be filled in the days DropDownList.
Implementation: Let's create an asp.net application to understand.
Description: So here in this example I have demonstrated with example How to fill/ load/ bind years, months and days in the DropDownList . Current date will be selected in the DropDownList by default and when user select another month from the DropDownList then corresponding number of days will be filled in the days DropDownList.
Implementation: Let's create an asp.net application to understand.
- In the design page(.aspx) place 3 DropDownList for year, month and days as:
<fieldset style="width:300px">
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>
- In the Code behind file(.aspx.cs) write the code as:
C#.NET Code to fill dropdownlist with days, months and years in asp.net
protected void
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Fill Years
for (int
i = 2013; i <= 2020; i++)
{
ddlYear.Items.Add(i.ToString());
}
ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected
= true; //set current year as selected
//Fill Months
for (int
i = 1; i <= 12; i++)
{
ddlMonth.Items.Add(i.ToString());
}
ddlMonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected
= true; // Set
current month as selected
//Fill days
FillDays();
}
}
public void
FillDays()
{
ddlDay.Items.Clear();
//getting numbner of days in selected month & year
int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));
//Fill days
for (int i = 1; i
<= noofdays; i++)
{
ddlDay.Items.Add(i.ToString());
}
ddlDay.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected
= true;// Set current
date as selected
}
protected void
ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
FillDays();
}
protected void
ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
FillDays();
}
FillDays();
}
- In the Code behind file(.aspx.vb) write the code as:
VB.NET Code to fill dropdownlist with days, months and years in asp.net
Protected Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Me.Load
If Not
Page.IsPostBack Then
'Fill Years
For i As
Integer = 2013 To
2020
ddlYear.Items.Add(i.ToString())
Next
ddlYear.Items.FindByValue(System.DateTime.Now.Year.ToString()).Selected
= True 'set current year as selected
'Fill Months
For i As
Integer = 1 To
12
ddlMonth.Items.Add(i.ToString())
Next
ddlMonth.Items.FindByValue(System.DateTime.Now.Month.ToString()).Selected
= True ' Set current month as selected
'Fill days
FillDays()
End If
End Sub
Public Sub FillDays()
ddlDay.Items.Clear()
'getting numbner of days in selected month & year
Dim noofdays As Integer = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue))
'Fill days
For i As Integer = 1 To
noofdays
ddlDay.Items.Add(i.ToString())
Next
ddlDay.Items.FindByValue(System.DateTime.Now.Day.ToString()).Selected = True ' Set current date as
selected
End Sub
Protected Sub
ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
FillDays()
End Sub
Protected Sub
ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
FillDays()
End Sub
Now over to you:
Now over to you:
"If you like my work; you can appreciate by leaving your comments,
hitting Facebook like button, following on Google+, Twitter, Linked in and
Pinterest, stumbling my posts on stumble upon and subscribing for receiving
free updates directly to your inbox . Stay tuned for more technical
updates."
18 comments
Click here for commentsDo you have a code for Month and Year Drop downs only... and what is the best way to save this data in sql server, should we use datetime to format it and save in a single field or add three different fields in database to store.. what is the best approach and what would you suggest. Thanks
ReplyThank you friend it was very much useful for me but simple change i want that is it has to display default as month,day,year not current then how to do.
ReplyHi Krishna Murthy..thanks for appreciating my work..i suggest you to read the article : http://www.webcodeexpert.com/2013/07/bindsaveeditupdatecanceldeletepaging.html
Replythis article has the current date filled..
Thanks very much
ReplyThanks
ReplyYour welcome Phearin Mey..stay connected and keep reading..:)
ReplyThanks
ReplyYour welcome..)
ReplyLalit Raghuvanshi excellent article, thanks for sharing. greetings from Monterrey Mexico
Replythanks for your feedback..i am glad you found this article useful...
ReplyBeautiful work, thank you.
ReplyDanny
Thanks Danny..keep reading..:)
ReplyAwesome...thats what I need....Thanks a lot :D
Replythank you so much for this code
ReplyThanks for your valuable feedback..stay connected and keep reading
Replythnks a lot
ReplyThanks ambadas for your valuable comment. Stay connected and keep reading for more useful updates..:)
ReplyIts very helpful!
ReplyThanks
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..