
本程序包含Default2.aspx、Default2.aspx.cs、CarsService.asmx、App_Code/CarsService.cs、CountrysService.xml五个文件;标红色的代码表示要注意的:
1、Default2.aspx完整代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<table>
<tr>
<td>Make</td>
<td><asp:DropDownList ID="DropDownList1" runat="server" Width="170" /></td>
</tr>
<tr>
<td>Model</td>
<td><asp:DropDownList ID="DropDownList2" runat="server" Width="170" /></td>
</tr>
<tr>
<td>Color</td>
<td><asp:DropDownList ID="DropDownList3" runat="server" Width="170" AutoPostBack="true"
OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged" /></td>
</tr>
</table>
<br />
<asp:XmlDataSource
ID="XmlDataSource1" runat="server" DataFile="~/App_Data/CountrysService.xml"></asp:XmlDataSource>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1"
Category="country" PromptText="Please select a make" LoadingText="[Loading makes...]"
ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents" />
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"
Category="state" PromptText="Please select a model" LoadingText="[Loading models...]"
ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="DropDownList1" />
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"
Category="university" PromptText="Please select a color" LoadingText="[Loading colors...]"
ServicePath="CarsService.asmx" ServiceMethod="GetDropDownContents"
ParentControlID="DropDownList2" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="inline">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="[No response provided yet]" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList3" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
2、Default2.aspx.cs完整代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Collections.Specialized;
using AjaxControlToolkit;
public partial class Default2 : System.Web.UI.Page
{
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
// Get selected values
string make = DropDownList1.SelectedItem.Text;
string model = DropDownList2.SelectedItem.Text;
string color = DropDownList3.SelectedItem.Text;
// Output result string based on which values are specified
if (string.IsNullOrEmpty(make))
{
Label1.Text = "Please select a make.";
}
else if (string.IsNullOrEmpty(model))
{
Label1.Text = "Please select a model.";
}
else if (string.IsNullOrEmpty(color))
{
Label1.Text = "Please select a color.";
}
else
{
Label1.Text = string.Format("You have chosen a {0} {1} {2}. Nice University!", color, make, model);
}
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
{
return new CarsService().GetDropDownContents(knownCategoryValues, category);
}
}
3、添加CarsService.asmx不用改CarsService.asmx,App_Code/CarsService.cs完整代码:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Collections.Specialized;
/// <summary>
/// CarsService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class CarsService : System.Web.Services.WebService {
private static XmlDocument _document;
private static object _lock = new object();
// we make these public statics just so we can call them from externally for the
// page method call
//
public static XmlDocument Document
{
get
{
lock (_lock)
{
if (_document == null)
{
// Read XML data from disk
_document = new XmlDocument();
//_document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CarsService.xml"));
_document.Load(HttpContext.Current.Server.MapPath("~/App_Data/CountrysService.xml"));
}
}
return _document;
}
}
//Hierarchy层级的意思
public static string[] Hierarchy
{
get
{
//return new string[] { "make", "model" };
return new string[] { "country", "state" };
}
}
public CarsService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
{
// Get a dictionary of known category/value pairs
StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
// Perform a simple query against the data document
return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
}
}
4、CountrysService.xml完整:




















































5、ok!参考How Do I--www.asp.net---video 3