Vb.net – Redirect to new page when user select from a dropdown list in ASP.NET

asp.netdrop-down-menuvb.net

I created an asp.net web application (VB) with a dropdownlist that have lists of url to pages on the server. I am new to asp and VB. I have research different forums for solution and decided to ask for a specific solution to my problem.

Break down.
– I have a fully built page
– this page gets archive every two hrs to an archive folder(using a vbs)
– An XML file is generated with the file name and url (using a VBS)
– XMl is the datasource for the DDL.

What I want to accomplish is, when the user click an item from the DDL, they should be directed to that page.

After follow some of the suggestion from other forum and this one, nothing seems to work.

Once we dive into this, we will get some better understanding of any confusion.

  • The code-behind is VB, so i will prefered that language.

ASPX Page

enter code here

<%@ Page Title="Home" Language="vb" MasterPageFile="~/Site.Master"     AutoEventWireup="false"CodeBehind="Default.aspx.vb" Inherits="Status._Default" %>     <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content><asp:Content ID="BodyContent"runat="server" ContentPlaceHolderID="MainContent"></asp:Content>


<asp:XmlDataSource ID="statsXML" 
    runat="server" DataFile="~/Archive/Stats.xml" 
    XPath="Elements/Element" /> 
<asp:DropDownList ID="DropDownList1" runat="server" 
     DataSourceID="statsXML"
     DataTextField="Name" 
     DataValueField="Value" 
     AutoPostBack="True" 
     CssClass="rightCol"  />
<br />
<p>

    <asp:Table ID="Table1" runat="server" GridLines="Horizontal" Width="100%">
        <asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12">
            <asp:TableCell HorizontalAlign="Center" Text="Text here" BorderStyle="Solid" BorderWidth="0"
                ForeColor="White" BackColor="#006699"></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12">
            <asp:TableCell HorizontalAlign="Center" Text="Text here" BorderStyle="Solid"
                BorderWidth="0" ForeColor="White" BackColor="#006699"></asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    <br />
</p>
<asp:Table ID="Table2" runat="server" GridLines="both" Width="100%" BorderColor="Black">
    <asp:TableRow BorderWidth="1" BorderStyle="Solid" Font-Size="12" BorderColor="Black">
        <asp:TableCell Width="50%" HorizontalAlign="Center" Text="Enviroment" BorderStyle="Solid" BorderWidth="1"
            ForeColor="White" BackColor="#006699" BorderColor="Black"></asp:TableCell>
        <asp:TableCell Width="50%" HorizontalAlign="Center" Text="State" BorderStyle="Solid"
            BorderWidth="1" ForeColor="White" BackColor="#006699" BorderColor="Black"></asp:TableCell>
    </asp:TableRow>
        </asp:Table>`

Code-Behind

Public Class webform
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If Not Page.IsPostBack Then

        'End If
        'If Page.IsPostBack Then
        '    ' Response.Redirect(Me.DropDownList1.SelectedValue)
        ' End If
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, 
                                                     ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged    

        Response.Redirect(DropDownList1.SelectedItem.Value)

    End Sub

End Class

Best Solution

What you'll want to do is to first set AutoPostback to True on your DropDownList.

<asp:DropDownList ID="myDropDownlist" runat="server" AutoPostback="True" />

After that you should be able to handle the SelectedIndexChanged event for the DropDownList in your code-behind

Protected Sub myDropDownList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles myDropDownList.SelectedIndexChanged

   ' Lookup the selected item and redirect here
   ' (This assumes that you've bound your URL to redirect to the value of the item,
   ' and not the display text.  Use SelectedItem.Text if the URL is being displayed
   Response.Redirect(myDropDownList.SelectedItem.Value)

End Sub
Related Question