设计页面:
<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);
}
}