DropDownList Control in ASP.NET

Introduction & Demonstration

The DropDownList control enables us to display a list of options while requiring a minimum of screen real estate. A user can select only one option at a time when using this control.

The page given below illustrates how we can use the DropDownList control to display all the product titles from the PRO_LIST database table.

Drop.gif

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

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

<script runat="server">

    Protected Sub btnSubmit_Click(ByVal sender As ObjectByVal e As EventArgs)
        lblProducts.Text = ddlProducts.SelectedItem.Text
    End Sub

</script>
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">

    <div>

    <asp:DropDownList
        id="ddlProducts"
        DataSourceID="SqlDataSource1"
        DataTextField="TITLE"
        DataValueField="ID"
        Runat="server" />

    <asp:Button
        id="btnSubmit"
        Text="Submit"
        OnClick="btnSubmit_Click"
        Runat="server" />

    <hr />

    <asp:Label
        id="lblProducts"
        Runat="server" />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE] FROM [PRO_LIST]"></asp:SqlDataSource>

    </div>
    </form>
</body>
</
html>

The DropDownList control renders an HTML <select> tag. One problem with the HTML <select> tag is that it has an infinite z index. In other words, we can't place other objects, such as an absolutely positioned <div> tag, in front of a DropDownList control in a page.

HAVE A GREAT CODING!

Up Next
    Ebook Download
    View all
    Learn
    View all