Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I have created two dropdowns
From year and To Year
if the user select From year 1955 and to year to 1945 thats not ok?
i am using this code right now

C#
int startYear = 1950;
            var data = Enumerable.Range(startYear, DateTime.Now.Year - (startYear - 1));
                DpListFranArUtbildning.DataSource = data;
                DpListFranArUtbildning.DataBind();
                DpListFranArUtbildning.Items.Insert(0, "- Select -");

                DpListTillArUtbildning.DataSource = data;
                DpListTillArUtbildning.DataBind();
                DpListTillArUtbildning.Items.Insert(0, "- Select -");


so i would like to make so the user can only can take higher years from the first he select or the same year like from 1955 to 1955 and also from 1955 and >
but not from 2000 to 1995
Posted

You can do this by inserting the years in second dropdown at runtime..

eg.. if i chose 1955..then at runtime..insert years from 1955 to (1955+limit)..in second dropdown..!

This way user can't be mistaken..

But the page will be refreshed..as we are calling server side code!

For this, you can do the same with javascript..at client end!
 
Share this answer
 
v2
Comments
Kurac1 2-May-13 15:21pm    
hmm i dont really understand maybe som example ?
Abhijeet Mohan 2-May-13 15:26pm    
Its like,..

if user selected year 1955 from first dropdown..then in second dropdown put (1955,1956,...2013)

if he selected 1987 then put (1988,1989,....2013) in second dropdown

similarly,.. for nth year...bind from n to present year in second drop-down
Kurac1 2-May-13 16:01pm    
so u mean that i need to do like that for every year ?
Kurac1 2-May-13 16:06pm    
i dont get it do u maybe have som code example?
Abhijeet Mohan 2-May-13 19:45pm    
yes for every year..with code behind.!!

-use the event of selected index change..

-get the value of dropdown..and convert it to date(year) call it offset

-now bind all the years from offset to the present in second dropdown..

that's what m saying!
Providing you how to resolve this with simplest example possible.You can get overview and implement it.

Aspx page:
XML
<asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="1995" Selected="True" Value="1995" />
        <asp:ListItem Text="1996" Value="1996" />
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownList2" runat="server"  AutoPostBack="true"
        onselectedindexchanged="DropDownList2_SelectedIndexChanged">
        <asp:ListItem Text="1995" Selected="True" Value="1995" />
        <asp:ListItem Text="1996" Value="1996" />
        <asp:ListItem Text="1997" Value="1997" />
        <asp:ListItem Text="1998" Value="1998" />
    </asp:DropDownList>
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>

Code behind:

C#
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(DropDownList2.SelectedValue) < Convert.ToInt32(DropDownList1.SelectedValue))
            {
                Label3.Text = "error";

            }
            else
            {
                Label3.Text = "ok";
            }
        }

You can see that on changing the value of second drop down the code checks the two values,if the second one is lower then label shows text error,otherwise it displays ok.
 
Share this answer
 
v2

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