Data binding by filling list and drop-down list controls with the results of direct ADO.NET queries : DropDownList « Data Binding « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DataSource-based Binding</title>
</head>
<body>
    <div id="pageContent">
        <form id="form1" runat="server">
            <h2>Browse Customers</h2>
            <hr />

            <table><tr>
                <td valign="top"> 
                    <asp:ListBox ID="CustomerList" runat="server" Height="200px" Width="280px" />
                </td>
                <td valign="top"> 
                    <asp:DropDownList runat="server" ID="CountryList" AppendDataBoundItems="True" Width="130px">
                        <asp:ListItem Text="[All]" /> 
                    </asp:DropDownList>            
                    <br />
                    <asp:Button ID="CountriesButton" runat="server" Text="Get countries..." Width="130px"
                        OnClick="CountriesButton_Click" />
                </td>
            </tr></table>
            <hr />
            <asp:Button ID="CustomersButton" runat="server" Text="Get customers..." 
                OnClick="CustomersButton_Click" />
       </form>
   </div>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Default : System.Web.UI.Page
{
    protected void CountriesButton_Click(object sender, EventArgs e)
    {
        if (CountryList.Items.Count > 1)
            return;

        string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(connString))
        {
            string cmdText = "SELECT DISTINCT country FROM customers";
            SqlCommand cmd = new SqlCommand(cmdText, conn);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                CountryList.DataSource = reader;
                CountryList.DataTextField = "country";
                CountryList.DataBind();
            }
        }
    }
    protected void CustomersButton_Click(object sender, EventArgs e)
    {
        CustomerList.Items.Clear();

        string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        string cmdText1 = "SELECT CustomerID, CompanyName, Country FROM customers";
        string cmdText2 = "SELECT CustomerID, CompanyName, Country FROM customers WHERE country='{0}'";
        string cmdText = cmdText1;
        if (CountryList.SelectedIndex > 0)
            cmdText = String.Format(cmdText2, CountryList.SelectedValue);

        DataSet data = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(cmdText, connString);
        adapter.Fill(data);

        CustomerList.DataMember = "Table";
        CustomerList.DataTextField = "companyname";
        CustomerList.DataValueField = "customerid";
        CustomerList.DataSource = data;
        CustomerList.DataBind();
    }
}








19.13.DropDownList
19.13.1.Binding to a Data Source
19.13.2.Determining the Selected List Item: SelectedIndex, SelectedItem, SelectedValue
19.13.3.Master/Details form with a list control
19.13.4.Enabling Automatic PostBacks
19.13.5.Table record editor by DropDownList and ListBox
19.13.6.Link DropDownList with SqlDataSource and do the editing
19.13.7.Prevent overlapping edits.
19.13.8.Binding ArrayList to asp:DropDownList
19.13.9.Data binding by filling list and drop-down list controls with the results of direct ADO.NET queries
19.13.10.Repeater and DropDownList