你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / C专栏
C#三级省市区ajax联动控件,利用UpdatePanel,以及页面取值
 

[csharp]
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PCAControl.ascx.cs" Inherits="PCAControl" %> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
        <asp:DropDownList ID="ddlProvice" runat="server" AppendDataBoundItems="true" AutoPostBack="true" 
            OnSelectedIndexChanged="ddlProvice_SelectedIndexChanged"> 
            <asp:ListItem Text="-请选择省份-" Value=""></asp:ListItem> 
        </asp:DropDownList> 
        <asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true" 
            onselectedindexchanged="ddlCity_SelectedIndexChanged"> 
            <asp:ListItem Text="-请选择城市-" Value=""></asp:ListItem> 
        </asp:DropDownList> 
        <asp:DropDownList ID="ddlArea" runat="server"> 
            <asp:ListItem Text="-请选择县区-" Value=""></asp:ListItem> 
        </asp:DropDownList> 
    </ContentTemplate> 
</asp:UpdatePanel> 

[csharp]
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
 
public partial class PCAControl : System.Web.UI.UserControl 

 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            BindProvince(); 
        } 
    } 
 
    //加载省份 
    protected void BindProvince() 
    { 
        DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query("SELECT DISTINCT PROVINCE FROM SY_CITYCODE  GROUP   BY PROVINCE").Tables[0]; 
        ddlProvice.DataTextField = "PROVINCE"; 
        ddlProvice.DataValueField = "PROVINCE"; 
        ddlProvice.DataSource = dt; 
        ddlProvice.DataBind(); 
    } 
 
    //加载城市 
    protected void ddlProvice_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        string SelectPro = ddlProvice.SelectedValue; 
        if (!string.IsNullOrEmpty(SelectPro)) 
        { 
            ddlCity.Items.Clear(); 
            ddlCity.AppendDataBoundItems = true; 
            ddlCity.Items.Insert(0, new ListItem("-请选择城市-", "")); 
            DataTable dt1 = Maticsoft.DBUtility.DbHelperOra.Query("SELECT CITY FROM SY_CITYCODE Where PROVINCE='" + SelectPro + "'  Group By City").Tables[0]; 
            ddlCity.DataTextField = "CITY"; 
            ddlCity.DataValueField = "CITY"; 
            ddlCity.DataSource = dt1; 
            ddlCity.DataBind(); 
        } 
    } 
 
    //加载县区 
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        string SelectCity = ddlCity.SelectedValue; 
        if (!string.IsNullOrEmpty(SelectCity)) 
        { 
            ddlArea.Items.Clear(); 
            ddlArea.AppendDataBoundItems = true; 
            ddlArea.Items.Insert(0, new ListItem("-请选择县区-", "")); 
            DataTable dt2 = Maticsoft.DBUtility.DbHelperOra.Query("SELECT AREA FROM SY_CITYCODE Where CITY='" + SelectCity + "'").Tables[0]; 
            ddlArea.DataTextField = "AREA"; 
            ddlArea.DataValueField = "AREA"; 
            ddlArea.DataSource = dt2; 
            ddlArea.DataBind(); 
        } 
    } 

调用页面
protected void Button1_Click(object sender, EventArgs e) 

    string SelectedProv = ((DropDownList)PCAControl1.FindControl("ddlProvice")).SelectedValue; 
    string SelectedCity = ((DropDownList)PCAControl1.FindControl("ddlCity")).SelectedValue; 
    string SelectedArea = ((DropDownList)PCAControl1.FindControl("ddlArea")).SelectedValue; 
    MSCL.JsHelper.Alert(SelectedProv + "-" + SelectedCity + "-" + SelectedArea, Page); 

  推荐精品文章

·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录
·2023年10月目录
·2023年9月目录 
·2023年8月目录 

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089