views:

66

answers:

5

Hello guys, just need tips on how to make forms where request are submitted via AJAX with a loading progress image. I am using update panels with AJAX framework. I would like to know about the recommended approach. Through JQuery or AJAX toolkit ?

Please advice, examples would be an added bonus for me.

+3  A: 

1- Prepare a client side div with "display:none" style property. put your loading image inside.

2 - when the user or page submits a request, change that divs display property to "block".

3- Add some kind of "information received" sign to the response and check this response from the client side and then change that divs display property back to "none"

Heidi
That would not block tab and space bar I think.
Popo
A: 

I find it easier to just use jQuery for my AJAX. You can for example just add a loading images after the submit button while the form is being submitted and remove it after you get response from the server.

$('SelectorForYourForm').submit(function(){
var $this = $(this),
    loadImg = $('<img>').attr({ 
    src: 'path/To/Img',
    alt: 'Loading..'
});
$this.find('input[type=submit]').attr('disabled', 'disabled').after(loadImg);

$.ajax({
    type: "POST",
    url: "/Message.aspx/GetTemplate",
    data: data: $this.serialize(),
    success: function (data) {
        //Do what you need to do with the data.
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert('error geting data');
    },
    complete: function(){
        // remove the ajax loading image and enable the form again.
        $this.attr('disabled', '').next().remove();
    }
});

});

Bjarki Heiðar
i dont get that code - what is templateId? How is that submitting the form?
RPM1984
sorry about that I'm just reusing code that I had. I will take that out. I', just adding a templateid as a paramter for my ajax request.
Bjarki Heiðar
+1  A: 

I would like to know about the recommended approach

Well, that depends on what you are doing, what parts of the form are you updating, how big is the form, what values are you sending to the server.

Generally speaking, if you want to update something simple (dropdownlist, listbox, etc), youd generally use JavaScript (or jQuery) to call an AJAX-enabled web service. This way, you're only sending to the server the data it needs, things like ViewState/cookies are not sent over the wire. You also have full control over the pre/post execution events (so you can add your loading images, call the WS, then clear them).

However, if you want to asynchronously update an entire form (which has a lot of controls), you're probably right in using an UpdatePanel. Things like a GridView are a good case for an UpdatePanel (as you usually need to handle editing, binding and paging all asynchronously).

The progress image is made easy with the following code:

<ProgressTemplate>
   <img src="someloadingimage.gif" alt="Loading" />
</ProgressTemplate>

Stick that inside your UpdatePanel, and whenever an AJAX call is made, the loading image will be shown.

HTH

RPM1984
Thank you RPM1984. Your reply was really detailed and helped me a great deal. I am going for the 2nd approach (updating the whole form with lots of gridviews/dropdown lists etc). I guess a little mix of JQuery and updatepanel will solve my problem.
Popo
No problems, glad to help!
RPM1984
Popo
@Popo - that's an example where the UpdatePanel isn't that great, when you want to "tweak it". =) Unfortunately the ProgressTemplate shows the image ANYTIME an asynchronous update has taken place (regardless of who fired it). If you want to customise WHICH button shows the loading image, you'll need to do it with javascript. have a look at a question i posted: http://stackoverflow.com/questions/3571173/asp-net-register-script-after-partial-page-postback-updatepanel. I find out "which update panel fired postback" - you'll need to find out "which button fired postback"
RPM1984
+1  A: 

If you use JQuery for AJAX request then you can use the following events -

$.ajax({ url: "test.html",
 type: "GET",
 beforeSend: function(){
             -----load your loader here-----
             });,
 success: function(){
      ------remove your loader here -----------  
      Remaining code
      }});

You can also use POST. in above example i have used GET.

For detailed documentation you can refer - http://api.jquery.com/jQuery.ajax/

Alpesh
+1  A: 

Create a small plug-in for your loader like so.

$.fn.ShowLoader = function(on){
    switch(on)
    {
        case true:
            $(this).show();
        break;
        default:
            $(this).hide();
        break;
    }
}

then use the following:

$('form').submit(function(){
    var Form = $(this);

    $('.loader',Form).ShowLoader(true);

    //Gather some params
    Location = Form.attr('src');
    Data = Form.Serialize();

    $.post(Location,Data,function(result){
        result = result || false;
        if(result)
        {
             $('.loader',Form).ShowLoader(false); //Disable the loader
             //Process result
        }
    });
})

html would just be a regular form, with an image / div inside with the class of loader

RobertPitt