使用具有级联下拉菜单的Web服务

时间:2011-05-19 12:07:52

标签: asp.net ajax

以下是我的代码 -

 <asp:DropDownList ID="ddlCategories" runat="server" />
 <asp:CascadingDropDown ID="cddCategory" runat="server" ServicePath="~\Categories.asmx"
 ServiceMethod="GetCategories" TargetControlID="ddlCategories" Category="Category"
 PromptText="Please select a category" LoadingText="[Loading categories...]" />
 <br />

在我的Page_Load函数中,我有

{
        ddlCategories.DataBind();
}

我的GetCategories方法是

    [WebMethod]
    public CascadingDropDownNameValue[] GetCategories(
      string knownCategoryValues,
      string category)
    {
        List<CascadingDropDownNameValue> l = new List<CascadingDropDownNameValue>();
        l.Add(new CascadingDropDownNameValue("International", "1"));
        l.Add(new CascadingDropDownNameValue("Electronic Bike Repairs & Supplies", "2"));
        l.Add(new CascadingDropDownNameValue("Premier Sport, Inc.", "3"));
        return l.ToArray();
    }

但是当加载页面时,永远不会调用GetCategories函数。我的ddlCategories下拉列表中有这些项目 - 请选择一个类别 [方法错误400]

我缺少一个步骤吗?

1 个答案:

答案 0 :(得分:0)

从查看CascadingDropDown sample和您的代码,我认为您可能会将属性设置略有错误。您的CascadingDropDown TargetControlId目前是ddlCategories,但我认为此值应该设置为ParentControlId属性,而您需要另一个 DropDownList,它将成为扩展程序的目标控件,例如

<asp:DropDownList ID="ddlCategories" runat="server" /><br/>
<asp:DropDownList id="ddlSubcategories" runat="server" />
<asp:CascadingDropDown ID="cddCategory" TargetControlID="ddlSubcategories" ParentControlId="ddlCategories" runat="server" ServicePath="~\Categories.asmx" 
ServiceMethod="GetCategories" Category="Category"  
PromptText="Please select a category" LoadingText="[Loading categories...]" />