Avatar of jsmithr
jsmithr
Flag 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
ASP.NETAJAXJavaScript

Avatar of undefined
Last Comment
jsmithr

8/22/2022 - Mon
Arnold Layne

Is this code sending out data or does it simply need to be refreshed every few seconds?
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

Arnold Layne

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
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
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
jsmithr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.