CascadingDropDown扩展器控件可以让页面中的若干个DropDownList控件产生联动的效果,即后一个DropDownList中的选项是根据用户在前面若干个DropDownList中的选择结果而动态生成的。
示例运行效果:
图(1)
图(2)
图(3)
CityFile.xml代码示例:
CascadingDropDown.aspx代码示例:
CascadingDropDown.aspx.cs代码示例:
CityFileService.asmx代码示例:
示例运行效果:
图(1)
图(2)
图(3)
CityFile.xml代码示例:
<?
xml version="1.0" encoding="utf-8"
?>
< CityService >
< country name ="中国" >
< province name ="福建" >
< city name ="福州" />
< city name ="厦门" />
< city name ="泉州" />
< city name ="漳州" />
< city name ="其他" />
</ province >
< province name ="广东" >
< city name ="广州" />
< city name ="深圳" />
< city name ="珠海" />
< city name ="中山" />
< city name ="其他" />
</ province >
</ country >
< country name ="其他" >
</ country >
</ CityService >
< CityService >
< country name ="中国" >
< province name ="福建" >
< city name ="福州" />
< city name ="厦门" />
< city name ="泉州" />
< city name ="漳州" />
< city name ="其他" />
</ province >
< province name ="广东" >
< city name ="广州" />
< city name ="深圳" />
< city name ="珠海" />
< city name ="中山" />
< city name ="其他" />
</ province >
</ country >
< country name ="其他" >
</ country >
</ CityService >
CascadingDropDown.aspx代码示例:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="CascadingDropDown.aspx.cs" Inherits="Chapter09_CascadingDropDown" 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 > CascadingDropDown Demo </ title >
< link href ="StyleSheet.css" rel ="stylesheet" type ="text/css" />
</ head >
< body >
< form id ="CascadingDropDownForm" runat ="server" >
< asp:ScriptManager ID ="sm" runat ="server" />
请选择您所在的城市:
< asp:DropDownList ID ="ddlCountry" runat ="server" />
< asp:DropDownList ID ="ddlProvince" runat ="server" />
< asp:DropDownList ID ="ddlCity" runat ="server" OnSelectedIndexChanged ="ddlCity_SelectedIndexChanged" AutoPostBack ="True" />< br />< br />
< asp:UpdatePanel ID ="up" runat ="server" >
< ContentTemplate >
< asp:Label ID ="lblResult" runat ="server" Text ="[No response provided yet]" />
</ ContentTemplate >
< Triggers >
< asp:AsyncPostBackTrigger ControlID ="ddlCity" EventName ="SelectedIndexChanged" />
</ Triggers >
</ asp:UpdatePanel >
< ajaxToolkit:CascadingDropDown ID ="ccdCountry" runat ="server"
TargetControlID ="ddlCountry"
Category ="Country"
PromptText ="请选择国家"
LoadingText ="国家加载中..."
ServicePath ="CityFileService.asmx"
ServiceMethod ="GetDropDownContents" />
< ajaxToolkit:CascadingDropDown ID ="ccdProvince" runat ="server"
TargetControlID ="ddlProvince"
Category ="Province"
PromptText ="请选择省份"
LoadingText ="省份加载中..."
ServiceMethod ="GetDropDownContentsPageMethod"
ParentControlID ="ddlCountry" />
< ajaxToolkit:CascadingDropDown ID ="ccdCity" runat ="server"
TargetControlID ="ddlCity"
Category ="City"
PromptText ="请选择城市"
LoadingText ="城市加载中..."
ServicePath ="CityFileService.asmx"
ServiceMethod ="GetDropDownContents"
ParentControlID ="ddlProvince" />
<!--
TargetControlID:该扩展器目标DropDownList控件的ID,即将联动的若干个DropDownList中某一个的ID
Category:该扩展器目标DropDownList在逻辑上的目录分类。该属性值将被传回到ServiceMethod属性所指定的Web Method中,
供我们在生成候选选项时使用
PromptText:该扩展器目标DropDownList控件的提示选择文字
LoadingText:该扩展器目标DropDownList控件在加载其中选项时显示的提示文字
ServicePath:提供联动DropDownList中候选值列表的Web Service的URL
ServiceMethod:提供联动DropDownList中候选值列表的Web Method的名称
ParentControlID:该扩展器目标DropDownList控件的上一个DropDownList的ID。只有用户选择了上一个DropDownList中的选项之后,
当前的DropDownList才会请求Web Service并显示出其返回的可选项目的列表,否则该DropDownList处于禁用状态
SelectedValue:该扩展器目标DropDownList控件的当前选中条目的值
-->
</ form >
</ body >
</ html >
<! 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 > CascadingDropDown Demo </ title >
< link href ="StyleSheet.css" rel ="stylesheet" type ="text/css" />
</ head >
< body >
< form id ="CascadingDropDownForm" runat ="server" >
< asp:ScriptManager ID ="sm" runat ="server" />
请选择您所在的城市:
< asp:DropDownList ID ="ddlCountry" runat ="server" />
< asp:DropDownList ID ="ddlProvince" runat ="server" />
< asp:DropDownList ID ="ddlCity" runat ="server" OnSelectedIndexChanged ="ddlCity_SelectedIndexChanged" AutoPostBack ="True" />< br />< br />
< asp:UpdatePanel ID ="up" runat ="server" >
< ContentTemplate >
< asp:Label ID ="lblResult" runat ="server" Text ="[No response provided yet]" />
</ ContentTemplate >
< Triggers >
< asp:AsyncPostBackTrigger ControlID ="ddlCity" EventName ="SelectedIndexChanged" />
</ Triggers >
</ asp:UpdatePanel >
< ajaxToolkit:CascadingDropDown ID ="ccdCountry" runat ="server"
TargetControlID ="ddlCountry"
Category ="Country"
PromptText ="请选择国家"
LoadingText ="国家加载中..."
ServicePath ="CityFileService.asmx"
ServiceMethod ="GetDropDownContents" />
< ajaxToolkit:CascadingDropDown ID ="ccdProvince" runat ="server"
TargetControlID ="ddlProvince"
Category ="Province"
PromptText ="请选择省份"
LoadingText ="省份加载中..."
ServiceMethod ="GetDropDownContentsPageMethod"
ParentControlID ="ddlCountry" />
< ajaxToolkit:CascadingDropDown ID ="ccdCity" runat ="server"
TargetControlID ="ddlCity"
Category ="City"
PromptText ="请选择城市"
LoadingText ="城市加载中..."
ServicePath ="CityFileService.asmx"
ServiceMethod ="GetDropDownContents"
ParentControlID ="ddlProvince" />
<!--
TargetControlID:该扩展器目标DropDownList控件的ID,即将联动的若干个DropDownList中某一个的ID
Category:该扩展器目标DropDownList在逻辑上的目录分类。该属性值将被传回到ServiceMethod属性所指定的Web Method中,
供我们在生成候选选项时使用
PromptText:该扩展器目标DropDownList控件的提示选择文字
LoadingText:该扩展器目标DropDownList控件在加载其中选项时显示的提示文字
ServicePath:提供联动DropDownList中候选值列表的Web Service的URL
ServiceMethod:提供联动DropDownList中候选值列表的Web Method的名称
ParentControlID:该扩展器目标DropDownList控件的上一个DropDownList的ID。只有用户选择了上一个DropDownList中的选项之后,
当前的DropDownList才会请求Web Service并显示出其返回的可选项目的列表,否则该DropDownList处于禁用状态
SelectedValue:该扩展器目标DropDownList控件的当前选中条目的值
-->
</ form >
</ body >
</ html >
CascadingDropDown.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 AjaxControlToolkit;
public partial class Chapter09_CascadingDropDown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
string country = ddlCountry.SelectedItem.Text;
string province = ddlProvince.SelectedItem.Text;
string city = ddlCity.SelectedItem.Text;
if (string.IsNullOrEmpty(country)) {
lblResult.Text = "请选择国家.";
}
else if (string.IsNullOrEmpty(province)) {
lblResult.Text = "请选择省份.";
}
else if (string.IsNullOrEmpty(city))
{
lblResult.Text = "请选择城市.";
}
else {
lblResult.Text = string.Format("<font color='blue'>{0}{1}省{2}市</font>", country, province, city);
}
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
{
return new CityFileService().GetDropDownContents(knownCategoryValues, category);
}
}
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 AjaxControlToolkit;
public partial class Chapter09_CascadingDropDown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
string country = ddlCountry.SelectedItem.Text;
string province = ddlProvince.SelectedItem.Text;
string city = ddlCity.SelectedItem.Text;
if (string.IsNullOrEmpty(country)) {
lblResult.Text = "请选择国家.";
}
else if (string.IsNullOrEmpty(province)) {
lblResult.Text = "请选择省份.";
}
else if (string.IsNullOrEmpty(city))
{
lblResult.Text = "请选择城市.";
}
else {
lblResult.Text = string.Format("<font color='blue'>{0}{1}省{2}市</font>", country, province, city);
}
}
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues, string category)
{
return new CityFileService().GetDropDownContents(knownCategoryValues, category);
}
}
CityFileService.asmx代码示例:
using
System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Specialized;
using System.Xml;
/// <summary>
/// CityFileService 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class CityFileService : System.Web.Services.WebService {
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/CityFile.xml"));
}
}
return _document;
}
}
public static string[] Hierarchy {
get {
return new string[] { "country", "province" };
}
}
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category) {
StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
}
}
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Specialized;
using System.Xml;
/// <summary>
/// CityFileService 的摘要说明
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class CityFileService : System.Web.Services.WebService {
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/CityFile.xml"));
}
}
return _document;
}
}
public static string[] Hierarchy {
get {
return new string[] { "country", "province" };
}
}
[WebMethod]
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category) {
StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);
}
}