<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>示范如何使用“级联下拉菜单”(CascadingDropDown)</title> </head> <body onload="focus();"> <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页面:
protected void ddlSubAreaName_SelectedIndexChanged(object sender, EventArgs e) { //将邮政编码显示在文字方块中。 this.TextBoxZipCode.Text = this.ddlSubAreaName.SelectedItem.Value; }
WebServices
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("select 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();
}
}
<asp:DropDownList ID="provinceID" runat="server"> </asp:DropDownList> <asp:DropDownList ID="cityID" runat="server"> </asp:DropDownList> <asp:DropDownList ID="streetID" runat="server"> </asp:DropDownList> <asp:DropDownList ID="emporiumID" runat="server"> </asp:DropDownList> <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" Category="Province" ServiceMethod="Province" ServicePath="../WebService/AreaService.asmx" TargetControlID="provinceID" LoadingText="请选择省份…" PromptText="请选择省份" SelectedValue=""> </ajaxToolkit:CascadingDropDown> <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" ParentControlID="provinceID" Category="City" ServiceMethod="City" ServicePath="../WebService/AreaService.asmx" TargetControlID="cityID" LoadingText="请选择城市…" PromptText="请选择城市" SelectedValue=""> </ajaxToolkit:CascadingDropDown> <ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" ParentControlID="cityID" Category="Street" ServiceMethod="Street" ServicePath="../WebService/AreaService.asmx" TargetControlID="streetID" LoadingText="请选择街道…" PromptText="请选择街道" SelectedValue=""> </ajaxToolkit:CascadingDropDown> <ajaxToolkit:CascadingDropDown ID="CascadingDropDown4" runat="server" ParentControlID="streetID" Category="Mall" ServiceMethod="Mall" ServicePath="../WebService/AreaService.asmx" TargetControlID="emporiumID" LoadingText="请选择商场…" PromptText="请选择商场" SelectedValue=""> </ajaxToolkit:CascadingDropDown> WebService代码 [WebMethod] public CascadingDropDownNameValue[] Province(string knownCategoryValues, string category) // <-除了ClientTypeRootList这个方法名可变动,其它不能变动 { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); // <-这段也得有,呵呵 string sql = "select id,name_cn from area where tid=1"; SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.DowntownConn, CommandType.Text, sql); List <CascadingDropDownNameValue> values = new List <CascadingDropDownNameValue> (); while (sdr.Read()) { values.Add(new CascadingDropDownNameValue(sdr["name_cn"].ToString(), sdr["id"].ToString())); } return values.ToArray(); } [WebMethod] public CascadingDropDownNameValue[] City(string knownCategoryValues, string category) // <-除了ClientTypeRootList这个方法名可变动,其它不能变动 { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); // <-这段也得有,呵呵 //二级客户的下拉列表,得取得一级的category的值,以下代码是判断上级列表的category值,存在或不是空的话把值赋给parentClientTypeID //RootClientType是一级CascadingDropDown设置的category属性名称 int parentClientTypeID; if (!kv.ContainsKey("Province") ¦ ¦ !Int32.TryParse(kv["Province"], out parentClientTypeID)) { return null; } string sql = "select id,name_cn from area where tid=" + parentClientTypeID; SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.DowntownConn, CommandType.Text, sql); List <CascadingDropDownNameValue> values = new List <CascadingDropDownNameValue> (); while (sdr.Read()) { values.Add(new CascadingDropDownNameValue(sdr["name_cn"].ToString(), sdr["id"].ToString())); } return values.ToArray(); } [WebMethod] public CascadingDropDownNameValue[] Street(string knownCategoryValues, string category) { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //三级客户的下拉列表,得取得二级的category的值,以下代码是判断上级列表的category值,存在或不是空的话把值赋给parentClientTypeID //RootClientType是二级CascadingDropDown设置的category属性名称 int parentClientTypeID; if (!kv.ContainsKey("City") ¦ ¦ !Int32.TryParse(kv["City"], out parentClientTypeID)) { return null; } string sql = "select s_id,name_cn from street where s_area=" + parentClientTypeID; SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DowntownConn, CommandType.Text, sql); List <CascadingDropDownNameValue> streetList = new List <CascadingDropDownNameValue> (); while (reader.Read()) { streetList.Add(new CascadingDropDownNameValue(reader["name_cn"].ToString(), reader["s_id"].ToString())); } return streetList.ToArray(); } [WebMethod] public CascadingDropDownNameValue[] Mall(string knownCategoryValues, string category) { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); //四级客户的下拉列表,得取得三级的category的值,以下代码是判断上级列表的category值,存在或不是空的话把值赋给parentClientTypeID //RootClientType是三级CascadingDropDown设置的category属性名称 int parentClientTypeID; if (!kv.ContainsKey("Street") ¦ ¦ !Int32.TryParse(kv["Street"], out parentClientTypeID)) { return null; } string sql = "select id,shopname_cn from twoshop where streetid=" + parentClientTypeID; SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.DowntownConn, CommandType.Text, sql); List <CascadingDropDownNameValue> mallList = new List <CascadingDropDownNameValue> (); while (reader.Read()) { mallList.Add(new CascadingDropDownNameValue(reader["shopname_cn"].ToString(), reader["id"].ToString())); } return mallList.ToArray(); }