Search code examples
asp.netajaxajaxcontroltoolkit

Calling Javascript After Web Method has fired


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"; }

Solution

  • 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.