How to restrict user to select month year in ascending order RRS feed

  • Question

  • User82362805 posted

    In Asp.net webform (C#) application I have a form which contains 2 dropdowns for month and year I need to restrict the user to select month and year in ascending order For example, if user select month and year 03/2018 he must be selected previous month in the same year. I already save monthandyear field in database which user select from dropdowns. 

    Kindly help me.

    Monday, June 17, 2019 7:39 AM

All replies

  • User409696431 posted

    Can you clarify the requirement?  Are you saying if a user has previously selected a month/year that they must select the next month?  If so, why offer that user a dropdown?

    Tuesday, June 18, 2019 1:51 AM
  • User288213138 posted

    Hi AfaqRajput,

    According to your description, I wrote a demo for you as a reference.

    I traverse the value in the DropdownList to compare it with the currently selected value.

    If it is larger than the currently selected month, set his property to disabled.

    The code:

                <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:DropDownList ID="DropDownList2" runat="server">
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

    protected void Button1_Click(object sender, EventArgs e)
    foreach (ListItem item in DropDownList1.Items)
    if (Convert.ToInt32(DropDownList1.SelectedValue)<Convert.ToInt32(item.Value))
    item.Attributes.Add("disabled", "disabled");
    foreach (ListItem item in DropDownList2.Items)
    if (Convert.ToInt32(DropDownList2.SelectedValue) != Convert.ToInt32(item.Value))
    item.Attributes.Add("disabled", "disabled");

    The result:

    Best regards,


    Tuesday, June 18, 2019 5:56 AM