实现省市县三级联动

  此例子是采用的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, Version=1.0.61025.0, Culture=neutral, 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, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </httpModules>

此时就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值