設計頁面:
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" Category="type" //注意這個type
PromptText="請選擇你要查看的類別"
ServicePath ="CascadingDropDownDemo.asmx" //獲取源數據的服務方法路徑
ServiceMethod="GetDropDownContents" //這個是方法,在上面那個文件
TargetControlID="DropDownList1" runat="server">
</ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" Category="name"
PromptText="請選擇你要查看的名稱"
ServicePath="CascadingDropDownDemo.asmx"
ServiceMethod="GetDropDownContents"
TargetControlID="DropDownList2"
ParentControlID="DropDownList1" runat="server">
</ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" Category="info"
PromptText="請選擇你要查看的信息"
ServicePath="CascadingDropDownDemo.asmx"
ServiceMethod="GetDropDownContents"
TargetControlID="DropDownList3"
ParentControlID="DropDownList2" runat="server">
</ajaxToolkit:CascadingDropDown>
CascadingDropDownDemo.asmx文件(是要重新建一個web服務文件)
這個實例是讀取xml文件的
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Collections.Specialized;
using System.Web.Script.Services;
/// <summary>
/// CascadingDropDownDemo 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]//這個添加,最後那個命名空間
public class CascadingDropDownDemo : System.Web.Services.WebService {
public CascadingDropDownDemo () {
//如果使用設計的組件,請取消註釋以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
private static XmlDocument _document;
private static object _lock = new object();
public static XmlDocument Document
{
get
{
lock (_lock)
{
if (_document == null)
{
_document = new XmlDocument();
_document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CascadingDropDownDemo.xml"));
}
}
return _document;
}
}
public static string[] Hierarchy
{
get { return new string[] { "type", "name" }; }
}
//獲取得列表數據
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
{
StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
}
}