加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

实现省市县三级联动

发布时间:2020-12-17 02:03:51 所属栏目:安全 来源:网络整理
导读:? 此例子是采用的webservice 实现的。首页在vs2005 中新建一个网站,添加一个web服务,此处的服务文件名为AreaService.asmx, 对应的cs 文件在app_code 下,AreaService.cs中的 代码如下 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding

? 此例子是采用的webservice 实现的。首页在vs2005 中新建一个网站,添加一个web服务,此处的服务文件名为AreaService.asmx,

对应的cs 文件在app_code 下,AreaService.cs中的 代码如下

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class AreaService : System.Web.Services.WebService
{
??? // Member variables
??? 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/AreaService.xml"));
??????????????? }
??????????? }
??????????? return _document;
??????? }
??? }

??? public static string[] Hierarchy
??? {
??????? get
??????? {

??????????? return new string[] { "province","city" };
??????? }
??? }

?
??? public AreaService()
??? {
??? }

??? /// <summary>
??? /// Helper web service method
??? /// </summary>
??? /// <param name="knownCategoryValues">private storage format string</param>
??? /// <param name="category">category of DropDownList to populate</param>
??? /// <returns>list of content items</returns>
??? [WebMethod]
??? public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues,string category)
??? {
??????? // Get a dictionary of known category/value pairs
??????? StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

??????? // Perform a simple query against the data document
??????? return AjaxControlToolkit.CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document,Hierarchy,knownCategoryValuesDictionary,category);
??? }
}

在App_Date 下有一个xml 文件,名为AreaService.xml,里面主要是各个省市县的数据,上面的代码的作用是:从xml文件中读出数据,利用 AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents去设置画面上第一个个下拉框中的值,

在此网站中添加web引用,把对应的解决方案中的web 服务引用上,然后在aspx页面上就可以这样写,但前提是开放环境中安装有ajax

,aspx页面上的

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

<%@ Register Assembly="System.Web.Extensions,Version=1.0.61025.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"
??? Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!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>无标题页</title>
</head>
<body>
??? <form id="form1" runat="server">
??
??????? <asp:ScriptManager ID="ScriptManager1" runat="server">
??????? </asp:ScriptManager>
???????
???? <table width="100%" border="0" cellpadding="0" cellspacing="0" style="margin-top:5px;background-color:#99CCFF">
???? <tr>
???? <td? style="height: 22px">
??????? <asp:UpdatePanel ID="UpdatePanel1" runat="server">
?????????? <ContentTemplate>
????????? <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="ddlProvince"
??????????? Category="province"? PromptText="请选择省"? LoadingText="[Loading provinces...]"
??????????? ServicePath="AreaService.asmx" ServiceMethod="GetDropDownContents" >
????????? </cc1:CascadingDropDown>
????????? <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlCity"
??????????? Category="city" PromptText="请选择市" LoadingText="[Loading citys...]"
??????????? ServiceMethod="GetDropDownContentsPageMethod" ParentControlID="ddlProvince">
????????? </cc1:CascadingDropDown>
????????? <cc1:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="ddlCounty"
??????????? Category="county" PromptText="请选择县区" LoadingText="[Loading countys...]"
??????????? ServicePath="AreaService.asmx" ServiceMethod="GetDropDownContents"
??????????? ParentControlID="ddlCity">
????????? </cc1:CascadingDropDown>
??????????? <asp:DropDownList ID="ddlProvince" runat="server" Width="100px" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged" AutoPostBack="true"/><asp:DropDownList ID="ddlCity" runat="server" Width="100px" /><asp:DropDownList ID="ddlCounty" runat="server" Width="100px" />
????????? </ContentTemplate>
?????????? <Triggers>
???????????????
??????????????? <asp:AsyncPostBackTrigger ControlID="ddlProvince" EventName="SelectedIndexChanged" />
??????????? </Triggers>
??????? </asp:UpdatePanel>
??????? </td>
???????? </tr>
? </table>
??? </form>
</body>
</html>
在对应的cs 文件中

?protected void ddlProvince_SelectedIndexChanged(object sender,EventArgs e)
??? {

??????? string province = ddlProvince.SelectedItem.Text;


??????? if (!string.IsNullOrEmpty(province))
??????? {
??????????? this.CascadingDropDown2.PromptText = "请选择市";
??????????? this.CascadingDropDown3.PromptText = "请选择县区";
??????????
??????? }

??? }
??? [WebMethod]
??? [System.Web.Script.Services.ScriptMethod]
??? public static CascadingDropDownNameValue[] GetDropDownContentsPageMethod(string knownCategoryValues,string category)
??? {
??????? AreaService cm = new AreaService();
??????? return cm.GetDropDownContents(knownCategoryValues,category);
??? }

但必须加上这二个命名引用using AjaxControlToolkit;
using System.Web.Services;
,此时可以生成网站,把刚才的前台页面运行一下,如果出现Sys 未定义的话,需要在web.Config 文件中添加以下的内容:

? <pages validateRequest="false">
????? <controls>
??????? <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions,PublicKeyToken=31bf3856ad364e35"/>
??????? <add tagPrefix="webdiyer" namespace="Wuqi.Webdiyer" assembly="AspNetPager"/>
????? </controls>
??? </pages>
??? <httpHandlers>
????? <remove verb="*" path="*.asmx"/>
????? <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory,System.Web.Extensions,PublicKeyToken=31bf3856ad364e35"/>
????? <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory,PublicKeyToken=31bf3856ad364e35"/>
????? <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler,PublicKeyToken=31bf3856ad364e35" validate="false"/>
??? </httpHandlers>
??? <httpModules>
????? <add name="ScriptModule" type="System.Web.Handlers.ScriptModule,PublicKeyToken=31bf3856ad364e35"/>
??? </httpModules>

此时就可以了。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读