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 :-
<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 :-
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()));
}
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();
}
}