• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 82
  • Last Modified:

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
0
jsmithr
Asked:
jsmithr
  • 4
  • 2
1 Solution
 
BobHavertyComhCommented:
Is this code sending out data or does it simply need to be refreshed every few seconds?
0
 
jsmithrAuthor Commented:
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

0
 
BobHavertyComhCommented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jsmithrAuthor Commented:
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
0
 
jsmithrAuthor Commented:
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
0
 
jsmithrAuthor Commented:
Using the asp.net timer worked perfectly. Slap the whole thing inside an update panel and it works smoothly.

<h3>Auto Refresh Gridview</h3>
    <asp:UpdatePanel ID="updPanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Timer ID="tmrAutoRefreshTimer" runat="server" Interval="5000" OnTick="tmrAutoRefreshTimer_Tick"></asp:Timer>
            <asp:GridView ID="gvAutoRefresh" runat="server" CssClass="gv" GridLines="None" AlternatingRowStyle-CssClass="gvalt" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField HeaderText="ID" DataField="ApplicationID" ItemStyle-Width="25" ReadOnly="true" />
                    <asp:BoundField HeaderText="Message" DataField="AuditMessage" ReadOnly="true" />
                    <asp:BoundField HeaderText="Created" DataField="CreateDate" DataFormatString="{0:d}" ItemStyle-Width="100" ReadOnly="true" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now