Get selected item in DropDownList : DropDownList « ASP.net Controls « ASP.NET Tutorial






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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        What is your favorite ice cream flavor? 
        <asp:DropDownList ID="flavors" runat="server">
            <asp:ListItem>A</asp:ListItem>
            <asp:ListItem>B</asp:ListItem>
            <asp:ListItem>C</asp:ListItem>
        </asp:DropDownList>
        <br /> 
        <asp:Button ID="btnSubmit" runat="server" Text="Click Me" />
        <br />
        <br />
        <asp:Label ID="results" runat="server"></asp:Label></div>
    </form>
</body>
</html>


File: Default.aspx.vb



Partial Class DropDownList
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        results.Text = "You like " & flavors.SelectedItem.Text
    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)