[code=C#<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.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>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">    <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
    </asp:ScriptManager>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
    
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
          <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
          
          </asp:DropDownList>
      </ContentTemplate>
      <Triggers>
          <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedValue"/>
      </Triggers>   
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
       <ContentTemplate>
           <asp:DropDownList ID="DropDownList3" runat="server">
           
           </asp:DropDownList>
       </ContentTemplate>
       <Triggers>
           <asp:AsyncPostBackTrigger ControlID="UpdatePanel1" EventName="Triggers" />
       </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>
][/code]using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = DB.creatBm256();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from bm_province", con);
        SqlDataReader sdr = cmd.ExecuteReader();
        this.DropDownList1.DataSource = sdr;
        this.DropDownList1.DataTextField = "province";
        this.DropDownList1.DataValueField = "proId";
        this.DropDownList1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = DB.creatBm256();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from bm_city where proId=" + DropDownList1.SelectedValue, con);
        SqlDataReader sdr = cmd.ExecuteReader();
        this.DropDownList2.DataSource = sdr;
        this.DropDownList2.DataTextField = "city";
        this.DropDownList2.DataValueField = "cityId";
        this.DropDownList2.DataBind();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = DB.creatBm256();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from bm_county where cityId=" + DropDownList2.SelectedValue.ToString(), con);
        SqlDataReader sdr = cmd.ExecuteReader();
        this.DropDownList3.DataSource = sdr;
        this.DropDownList3.DataTextField = "county";
        this.DropDownList3.DataValueField = "countyId";
        this.DropDownList3.DataBind();
    }
}
enentname那老是提示错,高手给指点下呗!

解决方案 »

  1.   

    1、AJAX中有两种Triggers:
    分别为AsyncPostBackTrigger和PostBackTrigger,
    AsyncPostBackTrigge用来指定某个服务器端控件以及其将触发的服务器端事件作为该UpdatePanel的异步更新触发器,它需要设置的属性有控件ID和服务端控件的事件;
    PostBackTrigger用来指定在UpdatePanel中的某个服务端控件,它所引发的回送不使用异步回送,而仍然是传统的整页回送。
    2、楼主可以这样试试:
    <Triggers>
        <asp:AsyncPostbackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged"/>
        <asp:AsyncPostbackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged"/>
    </Triggers>
      

  2.   

    以前也用 UpdatePanel  做过联动
    虽然是不刷新 但但选择的时候局部还是会小闪一下,感觉不是很爽 所以后来就自己写ajax了