省市区三级联动Ajax

Posted on 2008-12-19 15:19  Andylee  阅读(955)  评论(0编辑  收藏  举报

第一次用这东西来个总结,下面贴一下码!自定义控件中...

注意数据源的绑定,
似乎都和webservice有关,发现有四种可能导致错误:
1、webservice中的方法参数签名必须是string knownCategoryValues, string category似乎不能修改。
2、[System.Web.Script.Services.ScriptService]必须在类前面。
3、webservice路径错误。
4、应用方法名错误。

5、注意数据源的绑定

method error 500,犯的没有贴上ParentControlID还有就是数据源的问题

//------------------------------源码如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Zone.ascx.cs" Inherits="UserControl_Zone" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<table>
    <tr>
        <td colspan="3" rowspan="1" style="width: 343px">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
        </td>
    </tr>
    <tr>
        <td colspan="3" rowspan="3" style="width: 343px">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <table>
                        <tr>
                            <td style="width: 100px">
                                <asp:DropDownList ID="ddlProvince" runat="server" Width="100px">
                                </asp:DropDownList></td>
                            <td style="width: 100px">
                                <asp:DropDownList ID="ddlCity" runat="server" Width="100px">
                                </asp:DropDownList></td>
                            <td style="width: 150px">
                                <asp:DropDownList ID="ddlArea" runat="server" Width="100px">
                                </asp:DropDownList></td>
                        </tr>
                    </table>
                    <cc1:CascadingDropDown ID="cddProvince" runat="server" TargetControlID="ddlProvince" ServicePath="../ZoneWebServer.asmx" ServiceMethod="GetProvinceData" Category="Province" PromptText="请选择省份" LoadingText="省份加载中..">
                    </cc1:CascadingDropDown>
                    <cc1:CascadingDropDown ID="cddCity" runat="server" TargetControlID="ddlCity" ServicePath="../ZoneWebServer.asmx" ServiceMethod="GetCityData" Category="City" PromptText="请选择城市" LoadingText="城市加载中.." ParentControlID="ddlProvince">
                    </cc1:CascadingDropDown>
                    <cc1:CascadingDropDown ID="cddArea" runat="server" TargetControlID="ddlArea" ServicePath="../ZoneWebServer.asmx" ServiceMethod="GetAreaData" Category="Area" PromptText="请选择区县" LoadingText="区县加载中.." ParentControlID="ddlCity">
                    </cc1:CascadingDropDown>
                </ContentTemplate>
            </asp:UpdatePanel>
        </td>
    </tr>
    <tr>
    </tr>
    <tr>
    </tr>
</table>
&nbsp;&nbsp;

//-----------------------.cs


-----------------------------

zonewebserver

-----------------------------

 

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Collections.Specialized;
using AjaxControlToolkit;


/// <summary>
/// ZoneWebServer 的摘要说明
/// </summary>
[WebService(Namespace = "http://microsoft.com/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ZoneWebServer : System.Web.Services.WebService {

    public ZoneWebServer () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }
    /// <summary>
    /// 获取省数据
    /// </summary>
    /// <param name="zone"></param>
    /// <param name="zonecategory"></param>
    /// <returns></returns>
    [WebMethod]
    public CascadingDropDownNameValue[] GetProvinceData(string knownCategoryValues, string category)
    {
        List<CascadingDropDownNameValue> ProvinceList = new List<CascadingDropDownNameValue>();
        using(SqlConnection conn=new SqlConnection(SqlHelper.ConnectionStringLocalGS))
        {
            SqlParameter[] parm = new SqlParameter[] { };
            SqlDataReader sdrProvince = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "showProvince", parm);
            while(sdrProvince.Read())
            {
                ProvinceList.Add(new CascadingDropDownNameValue(sdrProvince["Province_Name"].ToString(),sdrProvince["Province_Tag"].ToString()));
            }
            return ProvinceList.ToArray();
        }
    }
    /// <summary>
    /// 获取市信息
    /// </summary>
    /// <param name="zone"></param>
    /// <param name="zonecategory"></param>
    /// <returns></returns>
    ///
    [WebMethod]
    public CascadingDropDownNameValue[] GetCityData(string knownCategoryValues, string category)
    {
        StringDictionary provincelist = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
         List<CascadingDropDownNameValue>  Citylist = new List<CascadingDropDownNameValue>();
         using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalGS))
         {
             SqlParameter[] parm = new SqlParameter[] {
            new SqlParameter("@ProvinceID",SqlDbType.NVarChar,500)
            };
             parm[0].Value = provincelist["province"];
             SqlDataReader sdrCity = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "showCity", parm);
             while (sdrCity.Read())
             {
                 Citylist.Add(new CascadingDropDownNameValue(sdrCity["City_Name"].ToString(), sdrCity["City_Tag"].ToString()));
             }
             return Citylist.ToArray();

         }
    }
    /// <summary>
    /// 获取区县
    /// </summary>
    /// <param name="zone"></param>
    /// <param name="zonecategory"></param>
    /// <returns></returns>
    ///
    [WebMethod]
    public CascadingDropDownNameValue[] GetAreaData(string knownCategoryValues, string category)
    {
        StringDictionary citylist = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        List<CascadingDropDownNameValue> Arealist = new List<CascadingDropDownNameValue>();
        using(SqlConnection conn=new SqlConnection(SqlHelper.ConnectionStringLocalGS))
        {
            SqlParameter[] parm = new SqlParameter[] {
            new SqlParameter("@CityID",SqlDbType.NVarChar)
            };
            parm[0].Value = citylist["City"];
            SqlDataReader sdrArea = SqlHelper.ExecuteReader(conn,CommandType.StoredProcedure,"ShowArea",parm);
            while(sdrArea.Read())
            {
                Arealist.Add(new CascadingDropDownNameValue(sdrArea["Area_Name"].ToString(),sdrArea["Area_ID"].ToString()));

            }
            return Arealist.ToArray();
        }
    }
   
}

 ---------------------

<%@ WebService Language="C#" CodeBehind="~/App_Code/ZoneWebServer.cs" Class="ZoneWebServer" %>

 

Copyright © 2024 Andylee
Powered by .NET 9.0 on Kubernetes