ajax控件工具包cascadingdropdown web服务错误c#

时间:2014-01-13 14:30:19

标签: c# ajaxcontroltoolkit cascadingdropdown

我正在尝试将ajax控件工具包cascadingdropdown与Web服务一起使用,但在使用Web服务时,我在初始下拉框中不断收到“方法错误0”。如果我在初始下拉时尝试不使用Web服务,那么第二次出现同样的错误(级联下拉)。

以下是我的Web服务代码和下拉列表:

     <asp:DropDownList ID="ServerDropDown" runat="server"></asp:DropDownList>

    <ajaxToolkit:CascadingDropDown
        ID="CascadingDropDown2"
        runat="server"
        TargetControlID="ServerDropDown" 
        Category="ServerID" 
        PromptText="-- Select one --"                         
        ServiceMethod="GetServerValues"
        ServicePath="../App_Data/Search.asmx.cs"
         />


    <asp:DropDownList ID="DatabaseDropDown" runat="server"></asp:DropDownList>

    <ajaxToolkit:CascadingDropDown
        ID="CascadingDropDown1"
        runat="server"
        TargetControlID="DatabaseDropDown" 
        ParentControlID="ServerDropDown"
        Category="DatabaseID" 
        PromptText="-- Select one --"             
        ServiceMethod="GetDatabasesForServer"
        ServicePath="../App_Data/Search.asmx.cs"
         />

服务代码:

using System;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;
using System.Collections.Generic;

///<summary>
/// Summary description for ServiceCS
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]


public class Search : System.Web.Services.WebService
{

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]    
    public CascadingDropDownNameValue[] GetDatabasesForServer(string knownCategoryValues, string category)
    {
        //StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        //int ServerID;
        //if (!kv.ContainsKey("Server") || !Int32.TryParse(kv["Server"], out ServerID))
        //{
        //    return null;
        //}

        string ServerID = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["ServerID"];

        string sSQL = @"SELECT DL.DatabaseID AS value, SL.ServerName + ' - ' + DL.DatabaseName AS DisplayText " +
                "FROM BabelFish.dbo.DatabaseList DL (NOLOCK) " +
                "INNER JOIN BabelFish.dbo.ServerList SL (NOLOCK) ON DL.ServerId = SL.ServerId " +
                "WHERE DL.IsActive = 1 " +
                "AND SL.ServerID = '" + ServerID + "' " +
                "Order by DisplayText";

        DataTable dt = DatabaseAccessing.DatabaseConnection.GetDataTable(sSQL);

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            values.Add(new CascadingDropDownNameValue((string)dr["DisplayText"], dr["DatabaseID"].ToString()));
        }

        //List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        //foreach (DataRow dr in ds)
        //{
        //    values.Add(new CascadingDropDownNameValue((string)dr["Color"], dr["ColorID"].ToString()));
        //}

        return values.ToArray();
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public CascadingDropDownNameValue[] GetServerValues(string knownCategoryValues, string category)
    {

        string sSQL = @"SELECT ServerId as Value, ServerName as DisplayText " +
            "FROM BabelFish.dbo.ServerList (NOLOCK) " +
            "Order By DisplayText";

        DataTable dt = DatabaseAccessing.DatabaseConnection.GetDataTable(sSQL);

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

        // tried manually adding if it was issue with data set but did not work eihter
        //values.Add(new CascadingDropDownNameValue("testdispally1", "1"));
        //values.Add(new CascadingDropDownNameValue("testdispally2", "2"));


        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            values.Add(new CascadingDropDownNameValue((string)dr["DisplayText"], dr["DatabaseID"].ToString()));
        }


        return values.ToArray();
    }



}// end class

1 个答案:

答案 0 :(得分:0)

一位朋友为我找到了解决方案。代码很好,但AjaxControlToolkit.dll没有正确注册(或其他东西)这个特定的功能。它已在工具包中注册了其他功能。他使用NUGET下载ajax工具包并将其作为独立软件包安装并指向软件包(而不是像我一样直接使用.dll)并且工作正常。

只需测试/确认此配置在发布到服务器时有效(我的.dll在服务器上发布的.dll注册路径时,根据.dll的注册路径无效)。