Search code examples
asp.netvb.netdropdownlistfor.aspxauth

Basic Website Web Form - How to link DropDownList to internal .aspx pages?


I'm relatively new to using VB, and am trying to do the following:

  1. Select DropDownList item upon Button click
  2. Link DropDownList items to internal pages

I feel like this is basic stuff, but am having a hard time with it.

Is this correct?

1 .

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="Answer.aspx">Answer</asp:ListItem>

2 .

<asp:Button ID="Button1" runat="server" Text="Button" onselectedindexchanged="DropDownList1_SelectedIndexChanged" />`

Solution

  • You will need to change your code a little bit to achieve this. First, you do not need the AutoPostback on the dropdown list unless you want it to hit the server when you select a new list item.

    On the button click, you will want to remove the

    OnSelectedIndexChange="DropDownList1_SelectedIndexChanged"
    

    And replace it instead with the button's OnClick event.

    OnClick = "Button1_Click"
    

    You will need to add your vb code to redirect to the new page. I'm more of a C# guy, but this should work.

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim LinkText as string
        LinkText = DropdownList1.SelectedItem.Value
        Response.Redirect(LinkText)
    End Sub