Thursday, June 5, 2014

Fill dropdownlist with days, month and year in asp.net

Introduction: In this article today I will explain how we can fill dropdownlist with days, month and year in asp.net

Description:


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.

Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment