1、新建一个AJAX Control Toolkit Website
2、把CarsService.xml(相当一数据库)添加到项目的App_Data文件夹。
3、“添加新项”--“CarsService.asmx”,在CarsService.cs添加代码:
using System.Xml;
using System.Web.Services;
using System.Collections.Specialized;
using AjaxControlToolkit;
[System.Web.Script.Services.ScriptService()]
private static XmlDocument _document;
private static object _lock = new object();
-------
public static XmlDocument Document
{
//code...
}
-------
public static string[] Hierarchy
{
//code...
}
4、然后把原来的HelloWorld方法改为 GetDropDownContents()方法
5、在Default.aspx添加一个updatepanel,再在updatepanel添加三个Dropdownlist
、三个CascadingDropDown和一个Label,把Dropdownlist3的“启用AutoPastBack”选中。
6、在Default.aspx.cs添加代码:
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string
knownCategoryValues, string category)
{
return new CarsService().GetDropDownContents(knownCategoryValues, category);
}
------------
7、CascadingDropDown1的属性窗口,把TargetControlID设为"DropDownList1";
CascadingDropDown2的TargetControlID设为"DropDownList2";
CascadingDropDown3的TargetControlID设为"DropDownList3"
8、把DropDownList1的Category设为"Make",LoadingText设为"[Loading makes...]",
PromptText设为"Please select a make",ServiceMethod设为"GetDropDownContents",
ServicePath设为"CarsService.asmx" ;DropDownList2和DropDownList3的设置类似:
具体看代码:
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server"
TargetControlID="DropDownList1"
Category="Make" PromptText="Please select a make" LoadingText="[Loading makes...]"
ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents" />
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server"
TargetControlID="DropDownList2"
Category="Model" PromptText="Please select a model" LoadingText="[Loading models...]"
ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server"
TargetControlID="DropDownList3"
Category="Color" PromptText="Please select a color" LoadingText="[Loading colors...]"
ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents"
ParentControlID="DropDownList2" />
-------
9、在在Default.aspx.cs添加代码:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
// code...
}
10、CarsService.cs的代码:
11、现在还不是开香槟的时候,因为当你选择了下拉框后Label却没有反馈东西,There Must Something Wrong!需要在Default.aspx的第一行添加代码:
EnableEventValidation="false"