views:

995

answers:

4

I have a Refresh button on my page that will perform an asp.net mvc ajax post to update some of the content on the page. The refresh/update process can be long running (about 10-20 seconds). Right now the user has to manually click the Refresh button to trigger the refresh. I want to trigger the refresh automatically every X minutes. Pretty much I want to trigger this same Ajax post as below (and it would be nice to display the LoadingElementId too).

<% using (Ajax.BeginForm("Refresh", null, 
                   new {userId=Model.UserId},
                   new AjaxOptions { UpdateTargetId = "UserForm", LoadingElementId = "RefreshingDiv" },
                   null))
               {
            %>

                    <button type="submit">
                        <img src="/zsys/img/icons/refresh.png" alt="Refresh" />
                        Refresh
                    </button>

            <% } %>

How can I force the asp.net mvc ajax postback?

A: 

jQuery timer + call $('#form').submit() should do the trick.

Wyatt Barnett
$('#RefreshForm').submit(); is doing a full postback and not ajax postback. Any ideas?
Jeff Widmer
Instead of submitting the form, I used jquery to click the refresh button: $('#RefreshFormButton').click();
Jeff Widmer
Good point--I tend to use jquery ajax form, and that overrides the submit method so I call stuff that way. But if you aren't doing that then calling your submit method makes lots of sense.
Wyatt Barnett
+1  A: 

Wyatt Barnett's answer made me try just clicking the Refresh button for the user. And then I used setInterval to trigger the Refresh Button click every 5 minutes:

    <script src="../../scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            setInterval("$('#RefreshFormButton').click()", 300000);
        });
    </script>
Jeff Widmer
A: 
<script type="text/javascript">
    $(document).ready(function() {
        setInterval("$('#RefreshFormButton').click()", 300000);
    });
</script>

Is above code working? not working for me............Any update?

jatin
A: 

Put the RefreshFormButton in the id submit button .... well you get the JS function to work.

voccioNetto