Jeff Widmer
Jeff Widmer

Reputation: 4876

Trigger an asp.net mvc ajax refresh automatically every X minutes

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?

Upvotes: 3

Views: 6162

Answers (3)

voccioNetto
voccioNetto

Reputation: 1

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

Upvotes: 0

Jeff Widmer
Jeff Widmer

Reputation: 4876

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>

Upvotes: 5

Wyatt Barnett
Wyatt Barnett

Reputation: 15673

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

Upvotes: 0

Related Questions