CascadingDropDown缺少参数:knownCategoryValues

时间:2011-02-04 09:10:15

标签: asp.net ajaxcontroltoolkit

我在使CascadingDropDown列表工作时遇到了一些麻烦,它在列表中显示了Method 500错误。我已经尝试了所有常见的解决方案,但仍然没有运气。

我最初收到以下错误:

  

System.ArgumentException:无效   方法名称'getcategories',   方法名称区分大小写。该   方法名称'GetCategories'   同名但不同   发现了套管。参数名称:   方法名

这是奇怪的,因为我肯定是在正确的情况下设置方法名称,但它是以小写形式发送(即使chrome显示页面在正确的情况下发送它)。无论如何我通过将方法名称本身更改为小写来解决这个问题。现在出现了一个新错误:

  

System.InvalidOperationException:   缺少参数:   knownCategoryValues。

如果有人能够解决这些问题中的任何一个会有很大的帮助,那么我在这个问题上花了太长时间。

感谢。

更新代码:

<ajaxToolkit:ToolkitScriptManager EnablePageMethods="true" ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:CascadingDropDown
    ID="CascadingDropDown1"
    runat="server"
    TargetControlID="mmCategory" 
    Category="Category" 
    PromptText="Select a category" 
    ServicePath="~/DropDownLists.asmx"
    ServiceMethod="GetCategories" />
<ajaxToolkit:CascadingDropDown
    ID="CascadingDropDown2"
    runat="server"
    TargetControlID="mmTemplate"
    ParentControlID="mmCategory"
    PromptText="Select a template"
    ServiceMethod="GetTemplates"
    ServicePath="~/DropDownLists.asmx"
    Category="Template" />
Category: <asp:DropDownList ID="mmCategory" runat="server"/><br/>
Template: <asp:DropDownList ID="mmTemplate" runat="server"/><br/>

网络服务文件:

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

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

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

    public DropDownLists()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

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

        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        values.Add(new CascadingDropDownNameValue("test 1", "1"));
        values.Add(new CascadingDropDownNameValue("test 2", "2"));
        return values.ToArray();

        /*using (MiscDataContext dc = new MiscDataContext())
        {
            var CatQuery = from D in dc.Templates
                        select new {  D.Category  }
                            into CatQueryResults
                                select CatQueryResults;

                foreach (var CatResult in CatQuery)
                {
                    string CatReturn = CatResult.Category;
                    values.Add(new CascadingDropDownNameValue(CatReturn, CatReturn));
                }
                return values.ToArray();
         }*/

    }


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

        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        string varCat;
        varCat = kv["Category"];


        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        values.Add(new CascadingDropDownNameValue("test 3", "3"));
        values.Add(new CascadingDropDownNameValue("test 4", "4"));
        return values.ToArray();
        /*using (MiscDataContext dc = new MiscDataContext())
        {
            var CatQuery = from D in dc.Templates
                        where(D.Category == varCat)
                        select new {  D.ID, D.Name  }
                        into CatResult
                        select CatResult;

                foreach (var CatResult in CatQuery)
                    {
                        int ID = (int)CatResult.ID;
                        string Name = CatResult.Name;
                        values.Add(new CascadingDropDownNameValue(Name, Convert.ToString(ID)));
                    }
                    return values.ToArray();
            }*/
        }
}

1 个答案:

答案 0 :(得分:0)

为了与CascadingDropDown一起使用,您的网络方法的原型必须是:

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

如果您为扩展器的ContextKey属性分配了一些内容,则该原型将成为:

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

knownCategoryValues参数表示主下拉列表中的当前选择。您应该将其传递给CascadingDropDown.ParseKnownCategoryValuesString()以获取其实际值。