I am having some trouble getting CascadingDropDown lists to work, its displaying the Method 500 error inside the list. I have tried all the common solutions to this and still no luck.
I was originally getting the following error:
System.ArgumentException: Invalid method name 'getcategories', method names are case sensitive. The method name 'GetCategories' with the same name but different casing was found. Parameter name: methodName
Which is odd because I am definately setting the method name in the correct case but it was sending it in lowercase (even though chrome showed the page as sending it in the correct case). Anyhow I worked around this by changing the method name itself to lowercase. This now brings up a new error:
System.InvalidOperationException: Missing parameter: knownCategoryValues.
If anyone could shed any light on either of these problems that would be a great help, I've spent way too long on this problem.
Thanks.
UPDATED Code:
<ajaxToolkit:ToolkitScriptManager EnablePageMethods="true" ID="ToolkitScriptManager1" runat="server">
</ajaxT开发者_开发问答oolkit: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/>
Web Service File:
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();
}*/
}
}
In order to be usable with a CascadingDropDown, your web method's prototype must be:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public CascadingDropDownNameValue[] GetCategories(
string knownCategoryValues, string category)
{
}
If you assign something to the ContextKey
property of the extender, that prototype becomes:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public CascadingDropDownNameValue[] GetCategories(
string knownCategoryValues, string category, string contextKey)
{
}
The knownCategoryValues
parameter represents the current selection in the master dropdown. You should pass it to CascadingDropDown.ParseKnownCategoryValuesString()
to obtain its actual value.
精彩评论