pulldown_nav.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim blnAutoNav As Boolean
If Not Page.IsPostBack Then
' Create a new ListItemCollection
Dim myLocations As New ListItemCollection()
' Add items to the collection
myLocations.Add(New ListItem("Where do you want to go?", Request.ServerVariables("URL")))
myLocations.Add(New ListItem("Code description and download", "http://www.asp101.com/samples/pulldown_nav_aspx.asp"))
myLocations.Add(New ListItem("View the live source code", "http://aspnet.asp101.com/samples/utils/source.aspx?file=pulldown_nav.aspx"))
myLocations.Add(New ListItem("ASP 101 home page", "http://www.asp101.com/"))
' Databind our DDL to the ListItemCollection we just filled
ddlLocations.DataSource = myLocations
ddlLocations.DataTextField = "Text"
ddlLocations.DataValueField = "Value"
ddlLocations.DataBind()
End If
' Find out current auto-navigation status and add JS to handle it if needed.
If Request.QueryString("auto") = "True" Then
blnAutoNav = True
' This adds the javascript to automatically redirect to the selected
' location whenever a new item is selected in the dropdownlist.
ddlLocations.Attributes("onchange") = "javascript:window.location = " _
& "document.frmNav.ddlLocations[document.frmNav.ddlLocations.selectedIndex].value;"
Else
blnAutoNav = False
End If
' Add the link to this page that toggles the autonav to
' the opposite of its current setting.
litAutoNavLink.Text = "<a href=""" & Request.ServerVariables("URL") _
& "?auto=" & CStr(Not(blnAutoNav)) & """>Toggle Auto Navigation</a>"
End Sub
Sub btnGo_Click(sender As Object, e As EventArgs)
Response.Redirect(ddlLocations.SelectedItem.Value)
End Sub
</script>
<html>
<head>
<title>ASP.NET DropDownList Navigation Sample</title>
</head>
<body>
<form id="frmNav" runat="server">
<asp:DropDownList id="ddlLocations" runat="server" />
<asp:Button id="btnGo" runat="server"
Text = "Go"
OnClick = "btnGo_Click"
/>
<p>
<asp:literal id="litAutoNavLink" runat="server" />
</p>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/pulldown_nav_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>