简体   繁体   English

ajax控制工具包cascadingdropdown Web服务错误C#

[英]ajax control toolkit cascadingdropdown web service error c#

I am trying to use the ajax control toolkit cascadingdropdown with a web service but I keep getting "Method Error 0" in the initial drop down box when using web service. 我正在尝试将Ajax控件工具包cascadingdropdown与Web服务一起使用,但是在使用Web Service时,在初始下拉框中始终出现“ Method Error 0”。 If I tried not using web service on initial drop down I got the same error on the second (cascading drop down). 如果我尝试在最初的下拉菜单中不使用Web服务,那么我在第二个下拉菜单中会遇到相同的错误(级联下拉菜单)。

Here is my code for the web service and the drop downs: 这是我的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"
         />

service code: 服务代码:

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

A friend found the solution for me. 一个朋友为我找到了解决方案。 The code was fine but the AjaxControlToolkit.dll was not registered correctly (or something) for this particular functionality. 代码很好,但是AjaxControlToolkit.dll没有针对此特定功能正确注册(或其他方式)。 It worked/was registered for other functionality in the toolkit. 它已在工具包中正常工作/已注册其他功能。 He used NUGET to download the ajax tool kit and install it as a stand alone package and point to the package (instead of directly to the .dll's as I was doing) and it worked fine. 他使用NUGET下载了ajax工具套件,并将其作为独立的软件包安装并指向该软件包(而不是像我刚才那样直接指向.dll),并且运行良好。

Just have to test/confirm this configuration works when posted to the server (I had issues with the .dll not working on the server based off the registered path for the .dll when it was posted to the server). 只需测试/确认此配置在发布到服务器时是否有效(我曾因.dll在发布到服务器时基于注册的.dll的注册路径而无法在服务器上工作时遇到问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM