Redirect to new page on ASP.Net DropDownList selection using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

I have a landing page with only dropdown select which a user can select any item and the user will be redirected to a new page by dropdown select, ant idea on how to do that?

Posted one month ago Modified on one month ago

Hey micah,

Please refer below sample.

HTML

CS.aspx

<div>
    Select Fruites Name :
    <asp:DropDownList runat="server" ID="ddlFruites" AutoPostBack="true" OnTextChanged="Redirect">
        <asp:ListItem Text="Select" />
        <asp:ListItem Text="Orange" />
        <asp:ListItem Text="Mango" />
        <asp:ListItem Text="Grapes" />
        <asp:ListItem Text="Banana" />
    </asp:DropDownList>
</div>

MessageCS.aspx

<div>
    <asp:Label ID="lblMessage" runat="server" />
</div>

Code

CS

CS.aspx.cs

protected void Redirect(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(ddlFruites.SelectedItem.Text))
    {
        Response.Redirect("MessageCS.aspx?value=" + ddlFruites.SelectedItem.Text);
    }
}

MessageCS.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    string value = Request.QueryString["value"];
    lblMessage.Text = "Select value is  : <b>" + value + "</b>";
}

VB.Net

VB.aspx.vb

 

Protected Sub Redirect(ByVal sender As Object, ByVal e As EventArgs)
    If Not String.IsNullOrEmpty(ddlFruites.SelectedItem.Text) Then
        Response.Redirect("MessageVB.aspx?value=" & ddlFruites.SelectedItem.Text)
    End If
End Sub

MessageVB.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim value As String = Request.QueryString("value")
    lblMessage.Text = "Select value is  : <b>" & value & "</b>"
End Sub