AjaxControlToolkit实现级联

来源:互联网 发布:大数据技术体系建设 编辑:程序博客网 时间:2024/05/22 10:08

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" onfocus="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" onfocus="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" onfocus="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;
        }

}


原创粉丝点击