我们简单的模拟上面功能,

<cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
Category="Province" //唯一性
PromptText="请选择省份..."
LoadingText="省份加载中..."
ServicePath="Country.asmx"
ServiceMethod="GetProvince"
ParentControlID="DropDownList1"
>
</cc1:CascadingDropDown>
<cc1:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
Category="City" PromptText="请选
择城市..." LoadingText="城市加载中..."
ServicePath="Country.asmx" ServiceMethod="GetCity" ParentControlID="DropDownList2">
</cc1:CascadingDropDown>
<cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
Category="Country" PromptText="请选择国家..." LoadingText="国家加载中..."
ServicePath="Country.asmx" ServiceMethod="GetCountry"
>
</cc1:CascadingDropDown>
下面是我们的服务方法
using AjaxControlToolkit;//需要我们添加
using System.Collections.Generic;
using System.Collections.Specialized;
/// <summary>
/// Country 的摘要说明
/// </summary>
[WebService(Namespace = "
http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class Country : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetCountry(string knownCategoryValues, string category)
//不能修改返回值,形参,唯一可以修改的是函数名,否则,就不能引用
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
List<CascadingDropDownNameValue> CountryList = new List<CascadingDropDownNameValue>();
CountryList.Add(new CascadingDropDownNameValue("中国","china"));
CountryList.Add(new CascadingDropDownNameValue("其他","Other"));
return CountryList.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetProvince(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
if(kv["Country"]!="china")
//就是前面的category值
return null;
List<CascadingDropDownNameValue> ProvList = new List<CascadingDropDownNameValue>();
ProvList.Add(new CascadingDropDownNameValue("安徽", "ah"));
ProvList.Add(new CascadingDropDownNameValue("北京", "bh"));
return ProvList.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCity(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
if (kv["Province"] != "ah")
return null;
List<CascadingDropDownNameValue> CityList = new List<CascadingDropDownNameValue>();
CityList.Add(new CascadingDropDownNameValue("安庆", "aq"));
CityList.Add(new CascadingDropDownNameValue("合肥", "bh"));
return CityList.ToArray();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
可能遇到问题:返回异常:
修改方法EnableEventValidation="false"
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ThreeDropDownList.aspx.cs" Inherits="ThreeDropDownList" EnableEventValidation="false" %>