Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DropDownList6.Items.Add(new ListItem("sweeper", "1"));
DropDownList6.Items.Add(new ListItem("watchman", "2")); 

if (DropDownList6.SelectedValue == "1")
{
  Response.Redirect("sweeper.aspx");
}
else if (DropDownList6.SelectedValue == "2")
{
  Response.Redirect("watchman.aspx");
}    


Append Data Bound :False
Auto Post Back : False
CausesValidation : False
Enabled : True
Enable Theming : True
Enable View State : True

here as i hav added first item as "sweeper" it is always going sweeper.aspx,
it is always redirecting to value=1 page only...,
is there any mistake in coding....???
Posted
Updated 14-Jun-11 22:00pm
v2

If you google for "DropDownList.SelectedValue" you will find this problem quite often: the answer is generaly allong the lines of: "Did you check IsPostBack when you loaded the DropDownList?"

Did you check? Because if you didn't...
 
Share this answer
 
The problem is very clear, that this dropdown is always binded and your first item became selected by default.

So binding logic should be in !IsPostBack
 
Share this answer
 
hi,Solution 2 is right,The binding logic should be in !IsPostBack
 
Share this answer
 
Hi,

I agree to solution 2 and solution 3. Alternatively you also add the following code:

DropDownList6.Items.Add(new ListItem("Please Select", "0"));

So now your dropdown list is loaded with Please Select option. The user would then need to select either Sweeper or Watchman from the dropdown list and re-directed to the appropriate page.
 
Share this answer
 
maybe your way has problem.
This is my way and it run is ok(it is Tested)
Auto Post Back : True(Checked)

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           ddl1.Items.Add(new ListItem(" ", "0"));
           ddl1.Items.Add(new ListItem("Trung", "1"));
           ddl1.Items.Add(new ListItem("tram", "2"));
       }
   }
   protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (ddl1.SelectedValue == "1")
       {
           Response.Redirect("Page1.aspx");
       }
       else if (ddl1.SelectedValue == "2")
       {
           Response.Redirect("Page2.aspx");
       }
   }



good lucky!!
 
Share this answer
 
yes i'm also agree with solution 2,3 and 4...
There is a problem in your binding logic..
i think you have add both value in page_load event
like...

MIDL
// logical problem 
private void Page_Load(object sender, EvnetArgs e)
{
    DropDownList6.Items.Add(new ListItem("sweeper", "1"));
    DropDownList6.Items.Add(new ListItem("watchman", "2"));
}

you should do like this.....
C#
//solution
private void Page_Load(object sender, EvnetArgs e)
{
   if(!IsPostback)
   {
       DropDownList6.Items.Add(new ListItem("sweeper", "1"));
       DropDownList6.Items.Add(new ListItem("watchman", "2"));
   }
}

To be more clear in this topic you should refer <a href="http://msdn.microsoft.com/en-us/library/ms178472.aspx">this</a>[<a href="http://msdn.microsoft.com/en-us/library/ms178472.aspx" target="_blank" title="New Window">^</a>] link
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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