用来附加到一个ASP.NET DropDownList控件之上,以便自动产生一整组DropDownList控件中的选项,以供用户选择。
属性:
TargetControlID | 指定要扩展的DropDownList的ID |
Category | DropDownList表示的类别名称,在WebMethod中会用到 |
PromptText | 没有选择时显示的文字 |
LoadingText | 加载数据时显示的文字 |
ServicePath | 获取数据的Web Service,为每个DropDownList都要指定 |
ServiceMethod | 获取数据的Web Method |
ParentControlID | 要扩展的DropDownList的父控件ID |
SelectedValue | 默认的选择项的值 |
实例代码:
aspx页面:
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
< head id ="Head1" runat ="server" >
< title > 示范如何使用“级联下拉菜单”(CascadingDropDown) </ title >
< link href ="style.css" type ="text/css" rel ="Stylesheet" />
</ head >
< body onload ="focus();" >
< div class ="banner" >
< a href ="http://abcdwxc.cnblogs.com/" target ="_blank" >
级联下拉菜单的使用(CascadingDropDown控件)------王晓成博客
</ a >
</ div >
< div class ="description" >
< ul >
< li > 从第一个下拉列表框选择某一个 < strong > “县市” </ strong > 之后,用来选择 < strong > “乡镇区市” </ strong > 的下拉列表框就可以进行选取。 </ li >
< li > 从第二个下拉列表框选择某一个 < strong > “乡镇区市” </ strong > 之后,便可以查看该乡镇区市的邮政编码。 </ li >
</ ul >
</ div >
< form id ="form1" runat ="server" >
< asp:ScriptManager ID ="ScriptManager1" runat ="server" >
</ asp:ScriptManager >
< center >
< table border ="1" width ="480px" >
< tr >
< td style ="width: 340px; height: 29px; text-align: right;" >
请选择县市: </ td >
< td style ="width: 140px; height: 29px;" >
< asp:DropDownList ID ="ddlCityName" runat ="server" >
</ asp:DropDownList ></ td >
</ tr >
< tr >
< td style ="width: 340px; text-align: right;" >
请选择乡镇区市: </ td >
< td style ="width: 140px" >
< asp:DropDownList ID ="ddlSubAreaName" runat ="server" AutoPostBack ="True"
onselectedindexchanged ="ddlSubAreaName_SelectedIndexChanged" >
</ asp:DropDownList ></ td >
</ tr >
< tr >
< td style ="width: 340px; text-align: right;" >
邮政编码: </ td >
< td style ="width: 140px" >
< asp:UpdatePanel ID ="UpdatePanel1" runat ="server" >
< ContentTemplate >
< asp:TextBox ID ="TextBoxZipCode" runat ="server" ></ asp:TextBox >
</ ContentTemplate >
< Triggers >
< asp:AsyncPostBackTrigger ControlID ="ddlSubAreaName" EventName ="SelectedIndexChanged" />
</ Triggers >
</ asp:UpdatePanel >
</ td >
</ tr >
</ table >
</ center >
< br />
< ajaxToolkit:CascadingDropDown ID ="CascadingDropDown1" runat ="server" Category ="City"
LoadingText ="读取县市数据中" PromptText ="请选择县市名称" ServiceMethod ="GetCityNames" ServicePath ="ZipCodeWebService.asmx"
TargetControlID ="ddlCityName" >
</ ajaxToolkit:CascadingDropDown >
< ajaxToolkit:CascadingDropDown ID ="CascadingDropDown2" runat ="server" Category ="SubArea"
LoadingText ="读取乡镇区市数据中" ParentControlID ="ddlCityName" PromptText ="请选择乡镇区市"
ServiceMethod ="GetSubAreaByCityID" ServicePath ="ZipCodeWebService.asmx" TargetControlID ="ddlSubAreaName" >
</ ajaxToolkit:CascadingDropDown >
</ form >
</ body >
</ html >
CS页面:
< head id ="Head1" runat ="server" >
< title > 示范如何使用“级联下拉菜单”(CascadingDropDown) </ title >
< link href ="style.css" type ="text/css" rel ="Stylesheet" />
</ head >
< body onload ="focus();" >
< div class ="banner" >
< a href ="http://abcdwxc.cnblogs.com/" target ="_blank" >
级联下拉菜单的使用(CascadingDropDown控件)------王晓成博客
</ a >
</ div >
< div class ="description" >
< ul >
< li > 从第一个下拉列表框选择某一个 < strong > “县市” </ strong > 之后,用来选择 < strong > “乡镇区市” </ strong > 的下拉列表框就可以进行选取。 </ li >
< li > 从第二个下拉列表框选择某一个 < strong > “乡镇区市” </ strong > 之后,便可以查看该乡镇区市的邮政编码。 </ li >
</ ul >
</ div >
< form id ="form1" runat ="server" >
< asp:ScriptManager ID ="ScriptManager1" runat ="server" >
</ asp:ScriptManager >
< center >
< table border ="1" width ="480px" >
< tr >
< td style ="width: 340px; height: 29px; text-align: right;" >
请选择县市: </ td >
< td style ="width: 140px; height: 29px;" >
< asp:DropDownList ID ="ddlCityName" runat ="server" >
</ asp:DropDownList ></ td >
</ tr >
< tr >
< td style ="width: 340px; text-align: right;" >
请选择乡镇区市: </ td >
< td style ="width: 140px" >
< asp:DropDownList ID ="ddlSubAreaName" runat ="server" AutoPostBack ="True"
onselectedindexchanged ="ddlSubAreaName_SelectedIndexChanged" >
</ asp:DropDownList ></ td >
</ tr >
< tr >
< td style ="width: 340px; text-align: right;" >
邮政编码: </ td >
< td style ="width: 140px" >
< asp:UpdatePanel ID ="UpdatePanel1" runat ="server" >
< ContentTemplate >
< asp:TextBox ID ="TextBoxZipCode" runat ="server" ></ asp:TextBox >
</ ContentTemplate >
< Triggers >
< asp:AsyncPostBackTrigger ControlID ="ddlSubAreaName" EventName ="SelectedIndexChanged" />
</ Triggers >
</ asp:UpdatePanel >
</ td >
</ tr >
</ table >
</ center >
< br />
< ajaxToolkit:CascadingDropDown ID ="CascadingDropDown1" runat ="server" Category ="City"
LoadingText ="读取县市数据中" PromptText ="请选择县市名称" ServiceMethod ="GetCityNames" ServicePath ="ZipCodeWebService.asmx"
TargetControlID ="ddlCityName" >
</ ajaxToolkit:CascadingDropDown >
< ajaxToolkit:CascadingDropDown ID ="CascadingDropDown2" runat ="server" Category ="SubArea"
LoadingText ="读取乡镇区市数据中" ParentControlID ="ddlCityName" PromptText ="请选择乡镇区市"
ServiceMethod ="GetSubAreaByCityID" ServicePath ="ZipCodeWebService.asmx" TargetControlID ="ddlSubAreaName" >
</ ajaxToolkit:CascadingDropDown >
</ form >
</ body >
</ html >
protected
void
ddlSubAreaName_SelectedIndexChanged(
object
sender, EventArgs e)
{
//将邮政编码显示在文字方块中。
this.TextBoxZipCode.Text = this.ddlSubAreaName.SelectedItem.Value;
}
编写相应的WebServices:
{
//将邮政编码显示在文字方块中。
this.TextBoxZipCode.Text = this.ddlSubAreaName.SelectedItem.Value;
}
using
System;
using System.Linq;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Script.Services;
using AjaxControlToolkit;
using System.Data;
using System.Web.Configuration;
using System.Configuration;
using System.Collections.Specialized;
/**/ /// <summary>
/// Summary description for ZipCodeWebService
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class ZipCodeWebService : System.Web.Services.WebService {
public ZipCodeWebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCityNames(string knownCategoryValues, string category)
{
//声明 CascadingDropDownNameValue 数组。
List<CascadingDropDownNameValue> values =new List<CascadingDropDownNameValue>();
//Dim values As New Generic.List(Of CascadingDropDownNameValue)
// 取得 web.config 中的数据库联机字符串设定来建立 SQL 联机对象。
string connectionString = ConfigurationManager.ConnectionStrings["County"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("SELECT CName, CID FROM County");
command.Connection = connection;
// 开启数据库连接并将数据读入数据读取器中。
connection.Open();
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
// 将「县市名称」与「县市代号」新增到数组中。
values.Add(new CascadingDropDownNameValue(sdr.GetSqlChars(0).Value.ToString(),sdr.GetInt32(1).ToString()));
}
return values.ToArray();
}
public CascadingDropDownNameValue[] GetSubAreaByCityID(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int cityID;
if (!kv.ContainsKey("cityID") || !Int32.TryParse(kv["cityID"], out cityID))
{
return null;
}
// 声明 CascadingDropDownNameValue 数组。
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
// 取得 web.config 中的数据库联机字符串设定来建立 SQL 联机对象。
string connectionString = ConfigurationManager.ConnectionStrings["County"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(@"ELECT VName, VCode FROM Village WHERE CountyID =" + cityID);
// 开启数据库连接并将数据读入数据读取器中。
connection.Open();
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
// 将「乡镇区市名称」与「邮政编码」新增到数组中。
values.Add(new CascadingDropDownNameValue(sdr.GetSqlString(0).ToString(), sdr.GetSqlString(1).ToString()));
}
return values.ToArray();
}
}
using System.Linq;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Script.Services;
using AjaxControlToolkit;
using System.Data;
using System.Web.Configuration;
using System.Configuration;
using System.Collections.Specialized;
/**/ /// <summary>
/// Summary description for ZipCodeWebService
/// </summary>
[WebService(Namespace = " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class ZipCodeWebService : System.Web.Services.WebService {
public ZipCodeWebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public CascadingDropDownNameValue[] GetCityNames(string knownCategoryValues, string category)
{
//声明 CascadingDropDownNameValue 数组。
List<CascadingDropDownNameValue> values =new List<CascadingDropDownNameValue>();
//Dim values As New Generic.List(Of CascadingDropDownNameValue)
// 取得 web.config 中的数据库联机字符串设定来建立 SQL 联机对象。
string connectionString = ConfigurationManager.ConnectionStrings["County"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("SELECT CName, CID FROM County");
command.Connection = connection;
// 开启数据库连接并将数据读入数据读取器中。
connection.Open();
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
// 将「县市名称」与「县市代号」新增到数组中。
values.Add(new CascadingDropDownNameValue(sdr.GetSqlChars(0).Value.ToString(),sdr.GetInt32(1).ToString()));
}
return values.ToArray();
}
public CascadingDropDownNameValue[] GetSubAreaByCityID(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int cityID;
if (!kv.ContainsKey("cityID") || !Int32.TryParse(kv["cityID"], out cityID))
{
return null;
}
// 声明 CascadingDropDownNameValue 数组。
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
// 取得 web.config 中的数据库联机字符串设定来建立 SQL 联机对象。
string connectionString = ConfigurationManager.ConnectionStrings["County"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(@"ELECT VName, VCode FROM Village WHERE CountyID =" + cityID);
// 开启数据库连接并将数据读入数据读取器中。
connection.Open();
SqlDataReader sdr = command.ExecuteReader();
while (sdr.Read())
{
// 将「乡镇区市名称」与「邮政编码」新增到数组中。
values.Add(new CascadingDropDownNameValue(sdr.GetSqlString(0).ToString(), sdr.GetSqlString(1).ToString()));
}
return values.ToArray();
}
}
运行结果: