Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have taken three drop-down lists namely Year,Month,Date.I have kept the month and date drop-down in hidden mode,when i select the year,then the month drop-down will show up and like-wise the date drop-down.My query is,i want to hide past month and date which have gone and i should be able to select on the months and dates which are to come in the future. Please through some guidance.

aspx.cs code :-

XML
<form id="form1" runat="server">
        <div>
            <br />
           <fieldset style="width:460px">
    <legend>Select Date</legend>
               <asp:Label ID="Label1" runat="server" Text="Year"></asp:Label>
     <asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="True" Visible="True"
            onselectedindexchanged="ddlYear_SelectedIndexChanged" ></asp:DropDownList>

               <asp:Label ID="Label2" runat="server" Text="Month" Visible="false"></asp:Label>
                <asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="True" Visible="false"
            onselectedindexchanged="ddlMonth_SelectedIndexChanged">
        </asp:DropDownList>
            <asp:Label ID="Label3" runat="server" Text="Date" Visible="false"></asp:Label>
    <asp:DropDownList ID="ddlDate" runat="server" Visible="false" OnSelectedIndexChanged="ddlDate_SelectedIndexChanged">
        </asp:DropDownList>
    </fieldset>
        </div>
    </form>


aspx code :-

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlYear.Items.Insert(0, new ListItem("Select a Year", "1"));
                for (int i = 2015; i <= 2020; i++)
                {
                    ddlYear.Items.Add(i.ToString());
                }

                ddlMonth.Items.Insert(0, new ListItem("Select a Month", "1"));
                var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
                for (int i = 1; i < 13; i++)
                {
                    ddlMonth.Items.Add(new System.Web.UI.WebControls.ListItem(DateTimeFormatInfo.CurrentInfo.GetMonthName(i), i.ToString()));
                    //ddlMonth.Items.Add(i.ToString());
                }

                FillDates();
            }
        }
        public void FillDates()
        {
            ddlDate.Items.Clear();
            ddlDate.Items.Insert(0, new ListItem("Select a Date", "1"));
            int noofdays = DateTime.DaysInMonth(Convert.ToInt32(ddlYear.SelectedValue), Convert.ToInt32(ddlMonth.SelectedValue));
            for (int i = 1; i <= noofdays; i++)
            {
                ddlDate.Items.Add(i.ToString());
            }

        }
        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDates();
            Label2.Visible = true;
            ddlMonth.Visible = true;
        }
        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDates();
            Label3.Visible = true;
            ddlDate.Visible = true;
        }
        protected void ddlDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDates();
        }
    }
Posted

1 solution

I would suggest you to use a Calendar Control. Either ASP.NET AjaxControlToolkit or jQuery.

That would give you these functionalities with quick easy properties or events. Please search any of the above in Google or Bing and refer the examples.
 
Share this answer
 
Comments
Haressh 2-Mar-15 5:12am    
Hey Tadit,thank you for the quick reply,appreciate it.I know about AjaxControlToolKit and jQuery and i have done using those but,but my concern is to disable the past month/date in the drop-down list.
If you use those controls, you have the option to disable previous dates. Please research. Otherwise, you have to write more code, which will not be efficient.

Still if you need it, I can say that just loop through the items and compare it with the current date/month/year whatever and do stuff.But, personally, I don't like this process.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900