I have a fairly simple ajax action link. The purpose of the action link is to fetch a partial view from the server and replace the contents of a div with new content.

The code looks something like:

<%= Ajax.ActionLink("click me", "GetThing", 
    new { Mode = "Simple"}, 
    new AjaxOptions { 
        UpdateTargetId = "thingDiv", 
        OnComplete = "completeGetThing"

As you can see, the action link just replaces the content of a div tag in the page (thingDiv) with new content from the server.

I also have a javascript function being called for the OnComplete event. The callback function here needs to do some things with the content that is loaded into thingDiv.

Here is a simplified example of what I'm doing in javascript:

function completeGetThing{
    if($("#thingDiv #subThing").length > -1){

I'm just using jQuery to fetch an element called "subThing" from within the div that we are updating.

But what I've discovered is that the OnComplete function appears to fire off before the HTML in thingDiv is actually replaced... so when my callback goes to look for the "subThing" element, it doesn't find it.

This is a VERY simplistic example of what I'm doing.

I am looking for good recommendation for how I might go about solving this problem, or better yet somone to point out something I've overlooked that can get the job done.


+1  A: 

You can try to find out if completeGetThing is actually running (put alert there). We had a problem with that and end up with using OnSuccess event instead of OnComplete. We are using this event to colorize loaded tables and it works.

Ok, I did some more checking and it appears the events fire in a different order than I'd have expected. In testing it appears that OnSuccess fires after OnComplete. Thanks for pointing me in the right direction.
Stephen M. Redd
+1  A: 

I use OnSuccess. Same as Sly and it works.

Thanks for the help ricardonns. I gave the answer flag to sly for being 1st to answer, but your confirmation of the answer was very helpful.
Stephen M. Redd