listbox 使用笔记

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

<!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>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="129px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
            SelectionMode="Multiple" Width="103px">
            <asp:ListItem>苹果</asp:ListItem>
            <asp:ListItem>柿子</asp:ListItem>
        </asp:ListBox><br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="添加" />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <br />
        <br />
        ==============<br />
        <asp:Label ID="Label2" runat="server" Text="省" Width="26px"></asp:Label>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList><br />
        <asp:Label ID="Label3" runat="server" Text="市" Width="27px"></asp:Label><asp:DropDownList
            ID="DropDownList2" runat="server">
        </asp:DropDownList><br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
   
    </div>
    </form>
</body>
</html>

======================================================


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class test_listbox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            SqlConnection con = DB2.createConnection();
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from province", con);
            SqlDataReader sdr = cmd.ExecuteReader();
            this.DropDownList1.DataSource = sdr;
            this.DropDownList1.DataTextField = "proName";
            this.DropDownList1.DataValueField = "proID";
            this.DropDownList1.DataBind();
            sdr.Close();
            //con.Close();

            SqlCommand cmdCity =new SqlCommand("select * from city where proID=" + this.DropDownList1.SelectedValue.ToString(),con);
            sdr = cmdCity.ExecuteReader();
            this.DropDownList2.DataSource = sdr;
            this.DropDownList2.DataValueField = "cityID";
            this.DropDownList2.DataTextField = "cityName";
            this.DropDownList2.DataBind();
            sdr.Close();

            con.Close();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Add(this.TextBox1.Text);
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "你已经选择了:" + this.ListBox1.SelectedItem.Text;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = DB2.createConnection();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from city where proID=" + this.DropDownList1.SelectedValue.ToString(), con);
        SqlDataReader sdr = cmd.ExecuteReader();
        this.DropDownList2.DataSource = sdr;
        this.DropDownList2.DataValueField = "cityID";
        this.DropDownList2.DataTextField = "cityName";
        this.DropDownList2.DataBind();
        sdr.Close();

        con.Close();
    }
}

下面一节课是: DropDownList 与 ListBox 的联动!

原文地址:https://www.cnblogs.com/gfwei/p/529852.html