页面范例2 - 邮政编码选取界面...完全真实数据!!!
图8-34所示的是页面范例CH8_DemoForm016.aspx的执行画面,这是一个完全真实数据的邮政编码选取界面,当然也是“级联下拉菜单”的最佳应用。请依照下列步骤来使用此界面:
1. 首先,如图8-35所示,从第一个下拉列表框中选取某一个县市。
2. 从第一个下拉列表框中选取某一个县市之后,该县市的乡镇区市名称会显示于第二个下拉列表框中,请接着从第二个下拉列表框中选取某一个乡镇区市,该乡镇区市的邮政编码就会显示在文本框中(如图8-36所示)。
从上述的操作说明可以了解,页面范例CH8_DemoForm016.aspx太实用了,至于其设计技巧,重点说明如下:
• 请在页面的源代码视图中,为@ Page 指示词添加以下的设定来关闭事件数据验证:
EnableEventValidation="false" |
请添加两个DropDownList控件,它们分别用来显示县市名称与乡镇区市名称。请务必将用来显示乡镇区市名称的DropDownList控件的AutoPostback属性设定成True,以便让其下拉菜单可以引发回送(Postback)。
请记得替显示乡镇区市名称的DropDownList控件的SelectedIndexChanged事件处理函数编写下列程序代码:
protected void ddlSubAreaName_SelectedIndexChanged(object sender, EventArgs e) { // 将邮政编码显示在文本框中。 this.TextBoxZipCode.Text = this.ddlSubAreaName.SelectedItem.Value; } |
请在页面中添加两个“级联下拉菜单”扩展器(也就是ASP.NET AJAX Control Tookit的CascadingDropDown控件),并如下所示设定它们的属性:
<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>
|
从以上的设定可以了解,第一个“级联下拉菜单”会调用Web服务ZipCodeWebService.asmx的GetCityNames方法来取得县市名称并以其作为第一个下拉列表框的选项;第二个“级联下拉菜单”则会调用Web服务ZipCodeWebService.asmx的GetSubAreaByCityID方法来取得用户所选取的县市名称下的乡镇区市名称并以其作为第二个下拉列表框的选项。
Web服务ZipCodeWebService.asmx的程序代码文档ZipCodeWebService.cs存放于App_Code文件夹中,其程序代码列示如下:
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; // 存取数据库所需的命名空间。 using System.Data; using System.Data.SqlClient; // 访问Web.config 所需的命名空间。 using System.Web.Configuration; // 导入ScriptServiceAttribute 类所需的命名空间。 using System.Web.Script.Services; // 返回CascadingDropDownNameValue 数组所需的命名空间。 using AjaxControlToolkit; using System.Collections.Generic; using System.Collections.Specialized; /// <summary> /// ZipCodeWebService 的摘要描述 /// </summary> [ScriptService()] [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class ZipCodeWebService : System.Web.Services.WebService { public ZipCodeWebService () { //如果使用设计的组件,请取消注释下行程序代码 //InitializeComponent(); } // 此Web 服务方法用来取得县市名称。 [WebMethod] public CascadingDropDownNameValue[] GetCityNames( string knownCategoryValues, string category) { // 声明CascadingDropDownNameValue 数组。 List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); // 取得web.config 中的数据库联机字符串设定来创建SQL联机对象。 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] public CascadingDropDownNameValue[] GetSubAreaByCityID( string knownCategoryValues, string category) { StringDictionary kcv =CascadingDropDown. ParseKnownCategoryValuesString(knownCategoryValues); // 是否包含City 的值。 if (!kcv.ContainsKey("City")) return null; // 声明CascadingDropDownNameValue 数组。 List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); // 取得web.config 中的数据库联机字符串设定来创建SQL联机对象。 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(); } } |
【责任编辑:
夏书 TEL:(010)68476606】