Link to home
Start Free TrialLog in
Avatar of jsmithr
jsmithrFlag for United States of America

asked on

Trying to update gridview in updatepanel using jquery every n seconds

I would like to update the following gridview every 5 seconds. I thought I could use javascript to call the postback, which runs, but never hits the code behind. I tried mapping this to a button and calling the .click event, but that did not work either. I think I am close -- can someone nudge me along?

    <asp:UpdatePanel ID="udPanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <h3>Waiting to Sync</h3>
            <asp:LinkButton ID="lnkbtnRefreshXREF" runat="server" Text="Refresh" ToolTip="Refresh" CssClass="syncbtn"></asp:LinkButton>
            <asp:GridView ID="gvXREF" runat="server" GridLines="None" CssClass="gv" AlternatingRowStyle-CssClass="gvalt" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true">
                <Columns>
                    <asp:BoundField HeaderText="Stop ID" DataField="STOP_ID" />
                    <asp:BoundField HeaderText="Customer ID" DataField="WHOLE_ACCOUNT_ID" />
                    <asp:BoundField HeaderText="Nickname" DataField="NICKNAME" />
                    <asp:BoundField HeaderText="Name" DataField="NAME" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

Open in new window


    <script>
        $(document).ready(function () {
            var t = 5
            if (t > 0) {
                // ### Start Page Reload Timer
                var iTimeRemaining = t;
                window.setInterval(function () {
                    if (iTimeRemaining == 0) {
                        //Post Back
                        setTimeout('__doPostBack(\'<%= lnkbtnRefreshXREF.ClientID%>\',\'\')', 0);
                    } else {
                        iTimeRemaining = iTimeRemaining - 1
                    }

                }, 1000);
            }
        });
    </script>

Open in new window


    Protected Sub lnkbtnRefreshXREF_Click(sender As Object, e As EventArgs) Handles lnkbtnRefreshXREF.Click
        PopulateLandmarkXREFGV()
    End Sub
Avatar of Arnold Layne
Arnold Layne
Flag of United States of America image

Is this code sending out data or does it simply need to be refreshed every few seconds?
Avatar of jsmithr

ASKER

Hi Bob,
Yes, this code actually binds sql data to the gridview:

Protected Sub lnkbtnRefreshXREF_Click(sender As Object, e As EventArgs) Handles lnkbtnRefreshXREF.Click
         PopulateLandmarkXREFGV() 
     End Sub 

Open in new window


    Private Sub PopulateLandmarkXREFGV()
        gvXREF.DataSource = objLMHelper.GetLandmarkXREF()
        gvXREF.DataBind()
    End Sub

Open in new window

Right, but are you sending form data out with any sort of submit button on that page? If you only want the already bound data to be refreshed, rather than sent, then meta refresh will do that for you in one line of code. It sounds to me like these bound fields are only retrieving what is in a database. So if you are not sending info from that page, and just want to request and display info from a DB at a set interval, then meta refresh will do exactly that.
Avatar of jsmithr

ASKER

I see what you are saying...
Yes, there are other functions on this page that the user submits. I did not include the entire markup for the page in this post.

I have another button on the page that submits form data to the server.
Did that answer your question?

So, I do not think meta refresh on the whole page will work.
- J
Avatar of jsmithr

ASKER

I found this article that uses and asp.net timer control. I have not tried to implement this, but will post my results when I do:

http://www.devcurry.com/2009/03/how-to-refresh-aspnet-gridview.html
ASKER CERTIFIED SOLUTION
Avatar of jsmithr
jsmithr
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial