小型雕刻:级联下拉菜单CascadingDropDown(调用web service)示例

来源:百度文库 编辑:中财网 时间:2024/05/06 17:32:48
第一个ddl未选择时第二个ddl不可用


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cascadingDropDown.aspx.cs" Inherits="cascadingDropDown" EnableEventValidation="false" %>



无标题页
























邮政编码选择界面

请选择县市:




请选择乡镇区市:

<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">


邮政编码:











Category="City" LoadingText="数据读取中..." PromptText="请选择县市名称" ServicePath="~/webservice/zipCode.asmx" ServiceMethod="GetCityNames" TargetControlID="DropDownList1">


ParentControlID="DropDownList1" ServicePath="~/webservice/zipCode.asmx" ServiceMethod="GetSubAreaByCityID" TargetControlID="DropDownList2">







+++++++++CS代码++++++++++++++
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = DropDownList2.SelectedValue;
}

++++++++++zipCode.cs++++++++++++++++++++++++++
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;
using AjaxControlToolkit;
using System.Collections.Specialized;
using System.Data;
///
///zipCode 的摘要说明
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class zipCode : System.Web.Services.WebService
{

public zipCode()
{

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

// 此 Web 服务方法用来获取县市名称。
[WebMethod(Description = "获取县市名称")]
public CascadingDropDownNameValue[] GetCityNames(
string knownCategoryValues, string category)
{// 声明 CascadingDropDownNameValue 阵列。
List values =
new List();
using (SqlConnection cn = new SqlConnection(WebConfigurationManager.
ConnectionStrings["chtNorthwind"].ConnectionString))
{
using (SqlCommand SQLCmd = cn.CreateCommand())
{
SQLCmd.CommandText = "SELECT 县市名称, 县市代号 FROM 县市 ORDER BY 2";
cn.Open();
using (SqlDataReader dr = SQLCmd.ExecuteReader())
{
while (dr.Read())
{
values.Add(new CascadingDropDownNameValue(
dr[0].ToString(), dr[1].ToString()));
}
}
}
}
return values.ToArray();
}


// 此 Web 服务方法用来获取乡镇区市。
[WebMethod(Description = "获取乡镇区市")]
public CascadingDropDownNameValue[] GetSubAreaByCityID(
string knownCategoryValues, string category)
{
StringDictionary kcv = CascadingDropDown.
ParseKnownCategoryValuesString(knownCategoryValues);
// 是否包含 City 的值。
if (!kcv.ContainsKey("City"))
return null;

// 声明 CascadingDropDownNameValue 阵列。
List values =
new List();
using (SqlConnection cn = new SqlConnection(WebConfigurationManager.
ConnectionStrings["chtNorthwind"].ConnectionString))
{
using (SqlCommand SQLCmd = cn.CreateCommand())
{
SQLCmd.CommandText ="SELECT 乡镇区市名称, 邮政编码 FROM 乡镇区市 " +
" WHERE 县市代号 = @CityID " +
" ORDER BY 1";
SQLCmd.Parameters.Add("@CityID", SqlDbType.Int).Value = kcv["City"];
cn.Open();
using (SqlDataReader dr = SQLCmd.ExecuteReader())
{
while (dr.Read())
{
values.Add(new CascadingDropDownNameValue(dr[0].ToString(), dr[1].ToString()));
}
}
}
}
return values.ToArray();
}
}