标签:
第一步:设置界面
1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="PCAControl.ascx.cs" Inherits="PCAControl" %> 2 <asp:ScriptManager ID="ScriptManager1" runat="server"> 3 </asp:ScriptManager> 4 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 5 <ContentTemplate> 6 <asp:DropDownList ID="ddlProvice" runat="server" AppendDataBoundItems="true" AutoPostBack="true" 7 OnSelectedIndexChanged="ddlProvice_SelectedIndexChanged"> 8 <asp:ListItem Text="-请选择省份-" Value=""></asp:ListItem> 9 </asp:DropDownList> 10 <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" 11 onselectedindexchanged="ddlCity_SelectedIndexChanged"> 12 <asp:ListItem Text="-请选择城市-" Value=""></asp:ListItem> 13 </asp:DropDownList> 14 <asp:DropDownList ID="ddlArea" runat="server"> 15 <asp:ListItem Text="-请选择县区-" Value=""></asp:ListItem> 16 </asp:DropDownList> 17 </ContentTemplate> 18 </asp:UpdatePanel>
第二步:C#写方法函数
1 using System.Collections.Generic; 2 using System.Linq; 3 using System.Web; 4 using System.Data; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 public partial class PCAControl : System.Web.UI.UserControl 9 { 10 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 if (!IsPostBack) 14 { 15 BindProvince(); 16 } 17 } 18 19 //加载省份 20 protected void BindProvince() 21 { 22 DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query("SELECT DISTINCT PROVINCE FROM SY_CITYCODE GROUP BY PROVINCE").Tables[0]; 23 ddlProvice.DataTextField = "PROVINCE"; 24 ddlProvice.DataValueField = "PROVINCE"; 25 ddlProvice.DataSource = dt; 26 ddlProvice.DataBind(); 27 } 28 29 //加载城市 30 protected void ddlProvice_SelectedIndexChanged(object sender, EventArgs e) 31 { 32 string SelectPro = ddlProvice.SelectedValue; 33 if (!string.IsNullOrEmpty(SelectPro)) 34 { 35 ddlCity.Items.Clear(); 36 ddlCity.AppendDataBoundItems = true; 37 ddlCity.Items.Insert(0, new ListItem("-请选择城市-", "")); 38 DataTable dt1 = Maticsoft.DBUtility.DbHelperOra.Query("SELECT CITY FROM SY_CITYCODE Where PROVINCE=‘" + SelectPro + "‘ Group By City").Tables[0]; 39 ddlCity.DataTextField = "CITY"; 40 ddlCity.DataValueField = "CITY"; 41 ddlCity.DataSource = dt1; 42 ddlCity.DataBind(); 43 } 44 } 45 46 //加载县区 47 protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e) 48 { 49 string SelectCity = ddlCity.SelectedValue; 50 if (!string.IsNullOrEmpty(SelectCity)) 51 { 52 ddlArea.Items.Clear(); 53 ddlArea.AppendDataBoundItems = true; 54 ddlArea.Items.Insert(0, new ListItem("-请选择县区-", "")); 55 DataTable dt2 = Maticsoft.DBUtility.DbHelperOra.Query("SELECT AREA FROM SY_CITYCODE Where CITY=‘" + SelectCity + "‘").Tables[0]; 56 ddlArea.DataTextField = "AREA"; 57 ddlArea.DataValueField = "AREA"; 58 ddlArea.DataSource = dt2; 59 ddlArea.DataBind(); 60 } 61 } 62 }
第三步:在页面中调用函数
1 protected void Button1_Click(object sender, EventArgs e) 2 { 3 string SelectedProv = ((DropDownList)PCAControl1.FindControl("ddlProvice")).SelectedValue; 4 string SelectedCity = ((DropDownList)PCAControl1.FindControl("ddlCity")).SelectedValue; 5 string SelectedArea = ((DropDownList)PCAControl1.FindControl("ddlArea")).SelectedValue; 6 MSCL.JsHelper.Alert(SelectedProv + "-" + SelectedCity + "-" + SelectedArea, Page); 7 }
02-26C#三级省市区ajax联动控件,利用UpdatePanel,以及页面取值
标签:
原文地址:http://www.cnblogs.com/xiaoqingshe/p/4300980.html