CascadingDropDown

来源:互联网 发布:淘宝店铺基本设置顺序 编辑:程序博客网 时间:2024/05/22 11:43

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Data;
using System.Collections.Specialized;
using System.Web.Services;


public partial class ajaxcontrol_cascdropdown : System.Web.UI.Page
{

    Province p = new Province();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DropDownList4.DataSource = p.GetProvinceList();
            DropDownList4.DataTextField = "provinceName";
            DropDownList4.DataValueField = "provinceID";
            DropDownList4.DataBind();
            DropDownList4.Items.Insert(0, new ListItem("请选择", "-1"));
        }
        else
        {
            DropDownList5.SelectedValue = HiddenField1.Value;
            DropDownList6.SelectedValue = HiddenField2.Value;
        }
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetProvinceList(string knownCategoryValues, string category)
    {
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        DataTable dt = new Province().GetProvinceList();
        foreach (DataRow dr in dt.Rows)
        {
            values.Add(new CascadingDropDownNameValue(dr["provinceName"].ToString(), dr["provinceID"].ToString()));
        }
        return values.ToArray();
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetCityList(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        string provinceId;
        if (kv.ContainsKey("provinceName"))
        {
            provinceId = kv["provinceName"];
        }
        else
        {
            return null;
        }
        DataTable dt = new Province().GetCityList(provinceId);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        foreach (DataRow dr in dt.Rows)
        {
            values.Add(new CascadingDropDownNameValue(dr["cityName"].ToString(), dr["cityID"].ToString()));
        }
        return values.ToArray();
    }

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static CascadingDropDownNameValue[] GetAreaList(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        string cityId;
        if (kv.ContainsKey("cityName"))
        {
            cityId = kv["cityName"];
        }
        else
        {
            return null;
        }
        DataTable dt = new Province().GetAreaList(cityId);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        foreach (DataRow dr in dt.Rows)
        {
            values.Add(new CascadingDropDownNameValue(dr["areaName"].ToString(), dr["areaID"].ToString()));
        }
        return values.ToArray();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = DropDownList4.SelectedValue + HiddenField1.Value + HiddenField2.Value;
    }
}
<asp:DropDownList ID="DropDownList1" runat="server" Width="100px"></asp:DropDownList>
    <asp:DropDownList ID="DropDownList2" runat="server" Width="100px"></asp:DropDownList>
    <asp:DropDownList ID="DropDownList3" runat="server" Width="100px"></asp:DropDownList>
   
    <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1" ServiceMethod="GetProvinceList" PromptText="请选择"
     Category="provinceName" SelectedValue="440000"></cc1:CascadingDropDown>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2" ParentControlID="DropDownList1"
     ServiceMethod="GetCityList" PromptText="请选择" Category="cityName" SelectedValue="440300"></cc1:CascadingDropDown>
    <cc1:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3" PromptText="请选择"
     ServiceMethod="GetAreaList" ParentControlID="DropDownList2" Category="areaName" SelectedValue="440304"></cc1:CascadingDropDown>