Sunday, March 11, 2012

CascadingDropDown Help

Hi,

It seems that you are using it in the right way.

And according to your description, it seems there is something wrong with your web service.

My suggestion is that you debug through the web service to see what's going on. It's easier to find the reason.


Yes, the problem was with the webservice. I rewrote it according to another example where you only use the webmethod for the second dropdown (use a regular objectdatasource for the main dropdown) as:

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;/// <summary>/// Summary description for WebService/// </summary>[WebService(Namespace ="http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService]public class WebService : System.Web.Services.WebService { public WebService () {//Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public CascadingDropDownNameValue[] GetStatesProvincesbyCountry(string knownCategoryValues, string category) { string[] categoryValues = knownCategoryValues.Split(':', ';'); int countryID = Convert.ToInt32(categoryValues[1]); List<CascadingDropDownNameValue> statesprovinces = new List<CascadingDropDownNameValue>(); ListsStateProvinceDatasetTableAdapters.ListsStateProvinceTableAdapter stateprovincesAdapter = new ListsStateProvinceDatasetTableAdapters.ListsStateProvinceTableAdapter(); foreach (DataRow dr in stateprovincesAdapter.GetStatesProvincesbyCountryID(countryID)) { statesprovinces.Add(new CascadingDropDownNameValue(dr["Description"].ToString(), dr["ItemID"].ToString())); }return statesprovinces.ToArray(); }}

and I rewrote my datasets to use:

PROCEDURE [ListsStateProvinceSelectCommandbyCountryID]
(
@.CountryID int
)
AS
SET NOCOUNT ON;
SELECT ItemID, ParentID, Description
FROM ViewListsStatesProvinces
WHERE (ParentID = @.CountryID)

Thanks!


I've used a bunch of two-level cascadingdropdowns in my project using the same structure as the above, and they all work perfectly. I have a situation where I need three-level cascadingdropdowns. Using my preivous example, the first two two dropdowns work properly, but the third does not get loaded after the selection of the second (it flashes but remains disabled and empty). I checked the webservice, and I can invoke it directly and get the correct XML. I've spent ages trying to figure it out, and I think I've read every single post on here relating to cascadingdrodowns, but I can't figure it out and it's driving me crazy!!

Thanks!

Edited Code:

 [WebMethod] (this is for the second DDL (ddlAffiliationSubType) and it works fine).public CascadingDropDownNameValue[] GetAffiliationSubTypebyAffiliationType(string knownCategoryValues,string category) {string[] categoryValues = knownCategoryValues.Split(':',';');int ParentID = Convert.ToInt32(categoryValues[1]); List child =new List(); ListsbyParentTableAdapters.ListsbyParentIDTableAdapter childAdapter =new ListsbyParentTableAdapters.ListsbyParentIDTableAdapter();foreach (DataRow drin childAdapter.GetDatabyParentID(20, ParentID)) { child.Add(new CascadingDropDownNameValue(dr["Description"].ToString(), dr["ItemID"].ToString())); }return child.ToArray(); }
 [WebMethod] (this is for the third DDL (ddlAffiliation) and it doesn't load).public CascadingDropDownNameValue[] GetAffiliationbyAffiliationSubType(string knownCategoryValues,string category) {string[] categoryValues = knownCategoryValues.Split(':',';');int ParentID = Convert.ToInt32(categoryValues[1]); List child =new List(); AffiliationbyTypeDatasetTableAdapters.AffiliationbyAffiliationSubTypeTableAdapter childAdapter =new AffiliationbyTypeDatasetTableAdapters.AffiliationbyAffiliationSubTypeTableAdapter();foreach (DataRow drin childAdapter.GetAffiliationbyAffiliationSubType(ParentID)) { child.Add(new CascadingDropDownNameValue(dr["AffiliationName"].ToString(), dr["AffiliationID"].ToString())); }return child.ToArray(); }
<asp:DropDownList ID="ddlAffiliationType" (first ddl) SkinID ="DropDownData" runat="server" DataSourceID="objListsAffiliationType" AppendDataBoundItems="true" SelectedValue='<%# Bind("AffiliationType")%>' DataTextField="Description" DataValueField="ItemID" > <asp:ListItem Text="(Select a Type)" Value="" /> </asp:DropDownList> <asp:DropDownList ID="ddlAffiliationSubType" (second ddl) SkinID ="DropDownData" runat="server" AppendDataBoundItems="true" DataTextField="Description"> <asp:ListItem Text="(Select a Sub Type)" Value="" /> </asp:DropDownList><asp:DropDownList ID="ddlAffiliation" (third dll - this is the one that doesn't load) SkinID ="DropDownData" runat="server" AppendDataBoundItems="true" > <asp:ListItem Text="(Select an Affiliation)" Value="" /> </asp:DropDownList><asp:ObjectDataSource ID="objListsAffiliationType" runat="server" OldValuesParameterFormatString="{0}" SelectMethod="GetLists" TypeName="ListsDatasetTableAdapters.ListsTableAdapter"> <SelectParameters> <asp:Parameter DefaultValue="16" Name="ListID" Type="Int32" /> </SelectParameters></asp:ObjectDataSource><cc1:CascadingDropDown ID="cddAffiliationSubType" runat="server" Category="Description" LoadingText="Loading..." ParentControlID="ddlAffiliationType" PromptText="(Select a Sub Type)" ServiceMethod="GetAffiliationSubTypebyAffiliationType" ServicePath="WebService.asmx" TargetControlID="ddlAffiliationSubType"></cc1:CascadingDropDown><cc1:CascadingDropDown ID="cddAffiliation" runat="server" Category="AffiliationName" LoadingText="Loading..." ParentControlID="ddlAffiliationSubType" PromptText="(Select an Affiliation)" ServiceMethod="GetAffiliationbyAffiliationSubType" ServicePath="WebService.asmx" TargetControlID="ddlAffiliation"></cc1:CascadingDropDown>

When I invoke WebService.asmx/GetAffiliationbyAffiliationSubType directly with "undefined: 28;", I get:

 - <ArrayOfCascadingDropDownNameValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">- <CascadingDropDownNameValue> <name>Oxford University</name> <value>2000</value> <isDefaultValue>false</isDefaultValue> </CascadingDropDownNameValue> </ArrayOfCascadingDropDownNameValue>

Try rebuilding your entire web site. I have frequently had to do that to get my changes to actually implement. Hope this helps.

No comments:

Post a Comment