Eason's .NET Space

只关注.NET
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用ASP.NET AJAX实现无刷新多个DropDownList的动态联动

Posted on 2007-02-13 14:47  Eason  阅读(1949)  评论(0)    收藏  举报

Default2.aspx

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

<!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>DDLajax</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
        
</asp:ScriptManager>
        
<br />
        
&nbsp;</div>
        
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            
<ContentTemplate>
        
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
            Width
="233px">
        
</asp:DropDownList><br />
        
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
            Width
="233px">
        
</asp:DropDownList><br />
                
<br />
        
<asp:Label ID="Label1" runat="server" Text="Label" Width="234px"></asp:Label>
            
</ContentTemplate>
        
</asp:UpdatePanel>
        
<br />
        
<br />
        
<br />
        
<br />
        
<marquee>This is a trace message that marces across the bottom of the screen.</marquee>
    
</form>
</body>
</html>

Default2.cs

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 Default2 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        DropDownList1.AutoPostBack 
= true;
        DropDownList2.AutoPostBack 
= true;

        
if (!IsPostBack)
        
{
            DropDownList1.Items.Add(
"Please Select Country");
            DropDownList2.Items.Add(
"Please Select State");

            DataRead(
"Select Country_ID, Country_Name from Country", DropDownList1);

            DropDownList2.Enabled 
= false;
            Label1.Text 
= "";
        }

        
else if (DropDownList1.SelectedIndex == 0)
            DropDownList2.Enabled 
= false;
    }


    
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add(
"Please Select State");

        
if (DropDownList1.SelectedIndex == 0)
            DropDownList2.Enabled 
= false;
        
else
        
{
            DropDownList2.Enabled 
= true;
            DataRead(
"Select State_ID, State_Name from State where State_Country = " + DropDownList1.SelectedValue, DropDownList2);
        }

        Label1.Text 
= "";
    }


    
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    
{
        Label1.Text 
= "You select " + this.DropDownList2.SelectedItem + "" + this.DropDownList1.SelectedItem;
        
if (DropDownList2.SelectedIndex == 0)
            Label1.Text 
= "";
    }


    
protected void DataRead(string Sqlstr, DropDownList ddl)
    
{
        SqlConnection conn 
= new SqlConnection("server=localhost;database=DropDownList;uid=sa;pwd=sa");
        conn.Open();
        SqlCommand com 
= new SqlCommand(Sqlstr, conn);
        SqlDataReader sr 
= null;
        sr 
= com.ExecuteReader();
        
while (sr.Read())
            ddl.Items.Add(
new ListItem(sr[1].ToString(),sr[0].ToString()));
        conn.Close();
    }

}

数据库

State_ID    State_Name                                         State_Country
----------- -------------------------------------------------- -------------
1           NewYork                                            1
2           Carifornia                                         1
3           Shanghai                                           2
4           Beijing                                            2
5           Tokyo                                              3
6           Osaka                                              3

(6 row(s) affected)

Country_ID  Country_Name                                      
----------- --------------------------------------------------
1           US
2           China
3           Japan

(3 row(s) affected)