1

I have a DropDownList with several options. I want the user to select an option and when the Next button is clicked, the page is redirected to its corresponding page. ex.
If the user chooses option A > Next button > Page A
if the user chooses option B > Next button > Page B
How exactly do I do this, please?

Additional Details:
There is only one DropDownList, values being populated from a connected database and one Next button.

Update:
I've used the below switch statement. It is redirecting but no matter what the choice is, it is always redirecting to the Birthday.aspx page.

switch (lstCategory.SelectedValue.ToString())
    {
        case "Birthday":
            Response.Redirect("Birthday.aspx");
            break;
        case "Christmas":
            Response.Redirect("Christmas.aspx");
            break;
        case "Valentine":
            Response.Redirect("Valentine.aspx");
            break;
    }

Problem Solved!
All I needed to do was Enable AutoPostBack from the DropDownList

1
  • Try javascript if all the optional pages are static pages or HTML pages, You can use html dropdoen too(type=Select) Commented Apr 25, 2012 at 12:36

7 Answers 7

1

you can try this.

in the click event of next button 1) see the value of the dropdownlist. if it is A, Response.Redirect to page A 2) if it is B, Response.Redirect to B

Sign up to request clarification or add additional context in comments.

2 Comments

that's what I tried to do. tried it with if/else and also with switch statement but it won't work. thanks for the reply :)
Try debugging. That should help. put a break point at your switch case. see how it is getting compared. Dont hesitate to try Server.Transfer instead of Response.Redirect. It may not work but still worthy trying. You can know what Server.Transfer is.
1
protected void Button_Click(object sender, EventArgs e)
    {
          switch(dropdownlist.SelectedValue) // or SelectedText
           {
             case "A": 
             Response.Redirect("A.aspx");
             break;
             case "B":
             Response.Redirect("B.aspx");
             break;
             default:
             Response.Redirect("NotFound.aspx");;
             break;
           }
    }

1 Comment

is there any errors? try dropdownlist.Text instead in switch statement
1

You could do something like this:

HTML:

<asp:DropDownList ID="DropDown1" runat="server">
    <asp:ListItem>Option A</asp:ListItem>
    <asp:ListItem>Option B</asp:ListItem>
</asp:DropDownList>

<asp:Button ID="Button1" runat="server" Text="Next" />

C# CodeBehind:

protected void Button1_Click(object sender, EventArgs e)
{
    string redirectTo = string.Empty;

    switch (DropDown1.SelectedIndex) {
        case 0:
            redirectTo = "PageA.aspx";
            break;
        case 1:
            redirectTo = "PageB.aspx";
            break;
    }

    Response.Redirect(redirectTo);
}

Comments

1

I am assuming u r new to stackoverflow and asp.net i would give you some online tutorials to refer.

Code Project

Ezine asp.net

Try something using this websites and you will understand how will you be able to make your requirement working

2 Comments

Yes, I'm new. Thanks for the reference, I'll check them out
You are welcome, try preparing a single webpage and studying the events of Dropdownlist. I can write the code down for you, but it will not help you to learn from it. Therefore i suggest you to create it yourself.
1

Basically you need:

1) create a button click event (double click on the button) 2) In this new event write something like that:

If(YourDropDownList.SelectedValue == 1){
Response.Redirect("http://www.SiteA.com");
}
else
{
Response.Redirect("http://www.SiteB.com");
}

If you post what you could do so far, it is easier try to help.

Hope it helps

Working code:

Body of my page:

<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="db" runat="server">
        <asp:ListItem>www.ademargomes.com</asp:ListItem>
        <asp:ListItem>www.google.com</asp:ListItem>
    </asp:DropDownList>

    <asp:Button runat="server" Text="Redirect" ID="bt" onclick="bt_Click"/>

    </div>
    </form>
</body>

Code behind button:

public partial class WebForm1 : System.Web.UI.Page

{ protected void Page_Load(object sender, EventArgs e) {

}

protected void bt_Click(object sender, EventArgs e)
{
    if (db.SelectedValue == "www.ademargomes.com")
    {
        Response.Redirect("http://www.ademargomes.com");
    }
    else
    {
        Response.Redirect("http://www.google.com");
    }
}

}

1 Comment

I tried it like this: protected void btnNext1_Click(object sender, EventArgs e) { switch (lstCategory.SelectedValue.ToString()) { case "Birthday": Response.Redirect("Birthday.aspx"); break; case "Christmas": Response.Redirect("Christmas.aspx"); break; }
0
  1. Keep dropdown list, next button or html link button

  2. write javascript to get the selected option of dropdown, And use if condition to and navigate pages as per the option selected.

  3. Incondition-> set the url's value property to the page link that u have.

Comments

0

An easy way to do this is by setting value of the ListItem to what you want such as :

<asp:DropDownList ID="ddlChoices" runat="server">
    <asp:ListItem Value="pageA.aspx" Text="OptionA" />
    <asp:ListItem Value="pageB.aspx" Text="OptionB" />
</asp:DropDownList>

<asp:Button ID="btnClickMe" runat="server" Text="Click Me" OnClick="btnClickMe_Clicked" />

Code behind

protected void btnClickMe_Clicked(object sender, EventArgs e) {
    Response.Redirect(ddlChoices.SelectedValue);
}

1 Comment

I wish I could but I was requested to populate the drop down list values from a database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.