张银的博客


Eat to live, but do not live to eat.

导航

下拉列表(DropDownList)

Posted on 2009-02-10 18:22  张银  阅读(799)  评论(0编辑  收藏  举报

演示一:
数据源使用数组列表的数据,改变选择时,激发一个事件

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
  OnSelectedIndexChanged
="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label" Width="125px"></asp:Label>

演示二:两级联动
使用SQL自带的pubs库的authors表,实现选择不同姓名后自动改变城市的联动效果(使用了SqlDataSourse控件)
        请选择姓名:<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="au_lname" DataValueField="au_id">
        
</asp:DropDownList>
        城市:
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource2" DataTextField="city" DataValueField="au_id">
        
</asp:DropDownList>
        
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsCon %>" SelectCommand="SELECT [au_id], [au_lname] FROM [authors]"></asp:SqlDataSource>
        
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:pubsCon %>" SelectCommand="SELECT [au_id], [city] FROM [authors] WHERE ([au_id] = @au_id)">
            
<SelectParameters>
                
<asp:ControlParameter ControlID="DropDownList2" Name="au_id" PropertyName="SelectedValue" Type="String" />
            
</SelectParameters>
        
</asp:SqlDataSource>
Web.config
    <connectionStrings>
        
<add name="pubsCon" connectionString="Data Source=.;Initial Catalog=pubs;User ID=i05;Password=i05630229" providerName="System.Data.SqlClient" />
    
</connectionStrings>

演示三:动态添加下拉列表的项
        <asp:DropDownList ID="DropDownList4" runat="server">
        
</asp:DropDownList>&nbsp;
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

以下是上面三个演示的后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;//因为要使用ArrayList
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;

public partial class DropDownList : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack) //是否首次加载(是否回发)
        {
            ArrayList arr1 
= new ArrayList();
            arr1.Add(
"请选择");
            arr1.Add(
"张银");
            arr1.Add(
"aidd2008");

            DropDownList1.DataSource 
= arr1;
            DropDownList1.DataBind(); 
//绑定
        }
    }
    
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
switch (DropDownList1.SelectedValue) //判断后激发事件
        { 
            
case "张银":
                Label1.Text 
= "选择了本名";
                
break;
            
case "aidd2008":
                Label1.Text 
= "选择了其他";
                
break;
            
default:
                Label1.Text 
= "没有选择";
                
break;
        }
    }
    
protected void Button1_Click(object sender, EventArgs e)
    {
        
string aa = TextBox1.Text.ToString();
        DropDownList4.Items.Add(aa); 
//添加文本框中的内容到下拉列表
    }
}