views:

39

answers:

3

Hi all,

I'm trying to implement the cascading dropdown from the toolkit. I need to get the count in a sub category dropdown, if it's zero then I turn off the visibility of the sub category.

If I use the javascript OnChange event then my script fires before the web method, so I need to know how to fire my script AFTER the web method has fired please.

My demo page: http://bit.ly/92RYvq

Below is my code and the order I need it to fire.

[WebMethod]
public CascadingDropDownNameValue[] GetSubCats1(string knownCategoryValues, string category)
{
    StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    int CategoryID;
    if (!kv.ContainsKey("Category") || !Int32.TryParse(kv["Category"], out CategoryID))
    {
        return null;
    }
    dsSubCat1TableAdapters.Categories_Sub1TableAdapter SubCats1Adapter = new dsSubCat1TableAdapters.Categories_Sub1TableAdapter();
    dsSubCat1.Categories_Sub1DataTable SubCats1 = SubCats1Adapter.GetSubCats1(CategoryID);

    List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
    foreach (DataRow dr in SubCats1)
    {
        values.Add(new CascadingDropDownNameValue((string)dr["SubCategory1"], dr["SubCatID1"].ToString()));
    }
    return values.ToArray();
}
function getSubCatCount() { $get("ddlSubCats1").style.display = $get("ddlSubCats1").length > 1 ? "block" : "none"; }
A: 

Use jQuery.ajax() it allows you to specify a success function and a failure function that fire after the web method returns.

Ben Robinson
I take it you mean jQuery, do you have a working example of that please? This is the code I want to run after the web method above:function getSubCatCount() { $get("ddlSubCats1").style.display = $get("ddlSubCats1").length > 1 ? "block" : "none"; } RegardsMelt
Melt
A: 

Generally when you call your web method function through javascript you can supply it two call back functions. One gets fired if there is an error and the other gets fire once the web method call has been completed. The callback requires two arguments, the result and a context. For example if your function was called myWebMethodFunction and your namespace it was contained in was my.fully.qualified.namespace it may look like this.

my.fully.qualified.namespace.myWebMethodFunction(param1, param2, ... , paramN, onErrorCallback, onCompleteCallback, context);  

Once that function finishes, it will call the onCompleteCallback passing the result of your webmethod function and whatever you passed for a context.
It has been a while since I've called a web method function so I might have gotten the order of the callback reversed.

For some reason I can't comment on things either, but I can add to this.
I may be thinking about something different, but you must be calling something through javascript to fire your webmethod, correct? Whatever you use to call the webmethod through javascript should provide a mechanism to add a callback that will be fired once your webmethod call is complete and returned.

Justin
@Justin: Melt replied to you via the answer system due to his reputation, just as an fyi.
Brian
A: 

For some reason I can't comment on your response Justin, so I have to add an answer.

Sorry Justin, you've totally lost me there.

This is my code on my aspx page that loads/calls the web method:

       <asp:CascadingDropDown ID="cddSubCats1" 

                                   runat="server" 
                                   TargetControlID="ddlSubCats1" 
                                   ParentControlID="ddlCats" 
                                   Category="SubCatsID1" 
                                   PromptText="Choose a Sub Category...." 
                                   LoadingText="Please wait ..." 
                                   ServicePath="PinkPagesService.asmx" 
                                   ServiceMethod="GetSubCats1">
            </asp:CascadingDropDown>

I'm not sure where you're reply fits into this. All I want is to execute my line of Javascript after

the web method has fired, can you help me do that, please?

Regards Melt

Melt