AjaxControlToolkit实现级联

1、首先下载AjaxControlToolkit

2、粘贴到Bin中

3、在.aspx前台的最上面加上引用<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

4、下面是.aspx中的代码

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

 <table border="0" cellpadding="0" cellspacing="1" width="100%">

 <tr>
                <td class="tdleft">
                    地址:
                </td>
                <td class="tdright">
                    <asp:DropDownList ID="ddlProvince1" runat="server" Width="80" οnfοcus="hiddenmessage('prv')"
                        CssClass="order5_text">
                    </asp:DropDownList>
                    <asp:CascadingDropDown ID="ccdProvince1" TargetControlID="ddlProvince1" Category="Province"
                        PromptText="请选择" LoadingText="省份加载中..." ServiceMethod="GetProvince" runat="server">
                    </asp:CascadingDropDown>
                    市
                    <asp:DropDownList ID="ddlCity1" runat="server" Width="80" οnfοcus="hiddenmessage('city')"
                        CssClass="order5_text">
                    </asp:DropDownList>
                    <asp:CascadingDropDown ID="ccdCity1" TargetControlID="ddlCity1" Category="City" PromptText="--------"
                        LoadingText="城市加载中..." ServiceMethod="GetCityForProvince" ParentControlID="ddlProvince1"
                        runat="server">
                    </asp:CascadingDropDown>
                    区\县
                    <asp:DropDownList ID="ddlArea1" runat="server" Width="80" οnfοcus="hiddenmessage('area')"
                        CssClass="order5_text">
                    </asp:DropDownList>
                    <asp:CascadingDropDown ID="ccdArea1" TargetControlID="ddlArea1" Category="Area" PromptText="--------"
                        LoadingText="区县加载中..." ServiceMethod="GetAreaForCity" ParentControlID="ddlCity1"
                        runat="server">
                    </asp:CascadingDropDown>
                    --<asp:TextBox ID="txtAddress" runat="server" Width="400px" Height="20px"></asp:TextBox>
                </td>
            </tr>

</table>

5、.cs中的代码

using AjaxControlToolkit;
using System.Collections.Generic;
using System.Collections.Specialized;


 if (!IsPostBack)
        {
            BindInit();  //加载绑定数据
        }

#region 省市级连
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetProvince(string knownCategoryValues, string category)
    {
        List<CascadingDropDownNameValue> countryList = new List<CascadingDropDownNameValue>();
        DataTable dt = DiQu.GetProvinceTable();
        foreach (DataRow dr in dt.Rows)
        {
            countryList.Add(new CascadingDropDownNameValue(dr["crcname"].ToString(), dr["crcode"].ToString()));
        }
        return countryList.ToArray();
    }
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetCityForProvince(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        List<CascadingDropDownNameValue> countryList = new List<CascadingDropDownNameValue>();
        DataTable dt = DiQu.GetCity(Convert.ToInt32(kv["Province"]));
        foreach (DataRow dr in dt.Rows)
        {
            countryList.Add(new CascadingDropDownNameValue(dr["crcname"].ToString(), dr["crcode"].ToString()));
        }
        return countryList.ToArray();
    }
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetAreaForCity(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        List<CascadingDropDownNameValue> countryList = new List<CascadingDropDownNameValue>();
        DataTable dt = DiQu.GetArea(Convert.ToInt32(kv["City"]));
        foreach (DataRow dr in dt.Rows)
        {
            countryList.Add(new CascadingDropDownNameValue(dr["crcname"].ToString(), dr["crcode"].ToString()));

        }
        return countryList.ToArray();
    }

//要初始化

 private void BindInit()

{

            this.ccdProvince1.SelectedValue = user.Address_ProvinceCode;
            this.ccdCity1.SelectedValue = user.Address_CityCode;
            this.ccdArea1.SelectedValue = user.Address_AreaCode;
}
    #endregion

6、获得地址的类

 public class DiQu
    {
        /// <summary>
        /// 返回省数据
        /// </summary>
        /// <param name="Cityid"></param>
        /// <returns></returns>
        public static string GetProvince()
        {
            getDiQu odiqu = new getDiQu();
            odiqu.crclass = 1;
            odiqu.crpcode = 0;
            DataTable oTable = odiqu.ExecDataTable(odiqu.ConnectionStringCustomer);

            StringBuilder buffer = new StringBuilder();
            buffer.Append(" var objProvince='");
            foreach (DataRow oRow in oTable.Rows)
            {
                buffer.Append(oRow["crcode"].ToString() + "," + oRow["crcname"].ToString() + ";");
            }
            string strJs = buffer.ToString().Substring(0, buffer.ToString().Length - 1) + "';\r\n";
            return "<script>\r\n" + strJs + "</script>\r\n";
        }
        /// <summary>
        /// 返回省数据
        /// </summary>
        /// <param name="Cityid"></param>
        /// <returns></returns>
        public static DataTable GetProvinceTable()
        {
            getDiQu odiqu = new getDiQu();
            odiqu.crclass = 1;
            odiqu.crpcode = 0;
            DataTable oTable = odiqu.ExecDataTable(odiqu.ConnectionStringCustomer);
            return oTable;
        }

        /// <summary>
        /// 返回市数据
        /// </summary>
        /// <param name="Cityid"></param>
        /// <returns></returns>
        public static DataTable GetCity(int province)
        {
            getDiQu odiqu = new getDiQu();
            odiqu.crclass = 2;
            odiqu.crpcode = province;
            DataTable oTable = odiqu.ExecDataTable(odiqu.ConnectionStringCustomer);
            return oTable;
        }
        /// <summary>
        /// 返回区数据
        /// </summary>
        /// <param name="Cityid"></param>
        /// <returns></returns>
        public static DataTable GetArea(int city)
        {
            getDiQu odiqu = new getDiQu();
            odiqu.crclass = 3;
            odiqu.crpcode = city;
            DataTable oTable = odiqu.ExecDataTable(odiqu.ConnectionStringCustomer);
            return oTable;
        }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值