locked
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:

     <div>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>3</asp:ListItem>
                    <asp:ListItem>4</asp:ListItem>
                    <asp:ListItem>5</asp:ListItem>
                    <asp:ListItem>6</asp:ListItem>
                    <asp:ListItem>7</asp:ListItem>
                    <asp:ListItem>8</asp:ListItem>
                    <asp:ListItem>9</asp:ListItem>
                    <asp:ListItem>10</asp:ListItem>
                    <asp:ListItem>11</asp:ListItem>
                    <asp:ListItem>12</asp:ListItem>
                </asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem>2010</asp:ListItem>
                    <asp:ListItem>2011</asp:ListItem>
                    <asp:ListItem>2012</asp:ListItem>
                    <asp:ListItem>2013</asp:ListItem>
                    <asp:ListItem>2014</asp:ListItem>
                    <asp:ListItem>2015</asp:ListItem>
                    <asp:ListItem>2016</asp:ListItem>
                    <asp:ListItem>2017</asp:ListItem>
                    <asp:ListItem>2018</asp:ListItem>
                    <asp:ListItem>2019</asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>

    Aspx.cs:
    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,

    Sam

    Tuesday, June 18, 2019 5:56 AM