Get selected index from asp:dropdownlist : DropDownList « ASP.net Controls « ASP.NET Tutorial






<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="MyPage" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 151px; position: absolute;
            top: 120px" Text="What is the date"></asp:Label>
        <asp:Label ID="result" runat="server" Height="25px" Style="z-index: 101; left: 159px;
            position: absolute; top: 242px" Text="Label" Width="324px"></asp:Label>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Height="48px"
            Style="z-index: 102; left: 274px; position: absolute; top: 118px" Width="243px">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Style="z-index: 104; left: 393px; position: absolute;
            top: 168px" Text="OK" />
    
    </div>
    </form>
</body>
</html>


File: Default.aspx.vb


Partial Class MyPage
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case Me.DropDownList1.SelectedIndex
            Case 0
                Me.result.Text = "A"
            Case 1
                Me.result.Text = "B"
            Case 2
                Me.result.Text = "C"
            Case 3
                Me.result.Text = "D"
            Case 4
                Me.result.Text = "E"
            Case 5
                Me.result.Text = "F"
            Case 6
                Me.result.Text = "G"
            Case 7
                Me.result.Text = "H"
            Case 8
                Me.result.Text = "I"
            Case 9
                Me.result.Text = "J"
            Case 10
                Me.result.Text = "K"
            Case 11
                Me.result.Text = "L"
        End Select

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            Me.DropDownList1.Items.Add("A")
            Me.DropDownList1.Items.Add("B")
            Me.DropDownList1.Items.Add("C")
            Me.DropDownList1.Items.Add("D")
            Me.DropDownList1.Items.Add("E")
            Me.DropDownList1.Items.Add("F")
            Me.DropDownList1.Items.Add("G")
            Me.DropDownList1.Items.Add("H")
            Me.DropDownList1.Items.Add("I")
            Me.DropDownList1.Items.Add("J")
            Me.DropDownList1.Items.Add("K")
            Me.DropDownList1.Items.Add("L")
        End If

    End Sub
End Class








3.15.DropDownList
3.15.1.Get selected item in DropDownList
3.15.2.Get selected index from asp:dropdownlist
3.15.3.Dynamically generating a DropDownList control from an array (C#)
3.15.4.Dynamically generating a DropDownList control from an array (VB)