Rika
Rika

Reputation: 29

Using ASP.Net C#, DropDownList and Redirecting?

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

Upvotes: 1

Views: 8232

Answers (7)

Ademar
Ademar

Reputation: 5747

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");
    }
}

}

Upvotes: 1

Simon Dugr&#233;
Simon Dugr&#233;

Reputation: 18916

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);
}

Upvotes: 0

Murtaza
Murtaza

Reputation: 3065

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

Upvotes: 1

Code Maverick
Code Maverick

Reputation: 20415

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);
}

Upvotes: 1

pvaju896
pvaju896

Reputation: 1417

  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.

Upvotes: 0

Zaki
Zaki

Reputation: 5600

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;
           }
    }

Upvotes: 1

Naga Sandeep
Naga Sandeep

Reputation: 1431

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

Upvotes: 1

Related Questions