LordBoBCUP
LordBoBCUP

Reputation: 103

ASP Multiple UpdatePanels with gridviews but only one updating

I wish to have 3 gridviews on a single aspx page fed by individual DB queries (displaying static data, no manipulation) and based on a 10 second timer, refresh the tables. I have the code to return the datatables sorted. I can make it update one gridview which is in one of my update panels, but the other two dont render.

Code is:

<%@ Page Title="Index" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="Test.Admin" %>
<script runat="server" type="text/c#">
    protected void Page_PreRender(object sender, EventArgs e)
    {
        Label1.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
        FullAccessSession.DataSource=GetStatus("FullAccess");
        FullAccessSession.DataBind();

        Label2.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
        LimitedAccessSession.DataSource=GetStatus("LimitedStatus");
        LimitedAccessSession.DataBind();

        Label3.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
        LogData.DataSource = GetLog() ;
        LogData.DataBind();

    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Label1.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
        FullAccessSession.DataSource=GetStatus("FullAccess");
        FullAccessSession.DataBind();

    }

    protected void Timer2_Tick(object sender, EventArgs e)
    {
        Label3.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
        LogData.DataSource = GetLog() ;
        LogData.DataBind();
    }

    protected void Timer3_Tick(object sender, EventArgs e)
    {
        Label2.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
        LimitedAccessSession.DataSource=GetStatus("LimitedStatus");
        LimitedAccessSession.DataBind();
    }



</script>

<div class="row">
    <div class="col-md-7">
        <asp:UpdatePanel ID="UpdateFullAccessStatus" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <!--<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
                </asp:Timer>-->
                <asp:Label ID="Label7" runat="server" Font-Bold="True" Text="Full Access Logged In Users"></asp:Label>
                <br />
                <asp:Label ID="Label1" runat="server" Text="Panel not refreshed yet."></asp:Label>
                <br />

                <asp:GridView ID="FullAccessSession" runat="server">

                </asp:GridView>
                <br />

            </ContentTemplate>
        </asp:UpdatePanel>

        <asp:UpdatePanel ID="UpdateLimitedAccessStatus" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <!--<asp:Timer ID="Timer3" runat="server" Interval="10000" OnTick="Timer2_Tick">
                </asp:Timer>-->

                <asp:Label ID="Label4" runat="server" Font-Bold="True" Text="Limited Access Logged In Users"></asp:Label>
                <br />
                <asp:Label ID="Label2" runat="server" Text="Panel not refreshed yet."></asp:Label>
                <br />
                <asp:GridView ID="LimitedAccessSession" runat="server">
                </asp:GridView>
                <br />


            </ContentTemplate>
        </asp:UpdatePanel>


        <asp:UpdatePanel ID="UpdateLog" runat="server" UpdateMode="Always">
            <ContentTemplate>

                <!--<asp:Timer ID="Timer2" runat="server" Interval="10000" OnTick="Timer2_Tick">
                </asp:Timer>-->

                <asp:Label ID="Label5" runat="server" Font-Bold="True" Text="General Log"></asp:Label>
                <br />

                <asp:Label ID="Label3" runat="server" Text="Panel not refreshed yet."></asp:Label>
                <asp:GridView ID="LogData" runat="server">
                </asp:GridView>

            </ContentTemplate>
        </asp:UpdatePanel>

    </div>
</div>

I cant work out why one of my update panels are working. As you can see i've tried using the PreRender function as well as (currently commented out) timers. The labels are updating with the current time but only one gridview displays.

Any help would be appreciated Thanks

Upvotes: 1

Views: 568

Answers (1)

Aristos
Aristos

Reputation: 66649

The issue here is that the script of timer is lost after the post back inside the UpdatePanel - the solution is to take it out of the update panel and use Triggers

Here is an example that I test it and works.

<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="2800"></asp:Timer>
<asp:Timer ID="Timer2" runat="server" ontick="Timer2_Tick" Interval="2500"></asp:Timer>

<div>
    <div>
    <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1"  />
        </Triggers>

        <ContentTemplate>                   
            <asp:Literal runat="server" ID="txtTest1"></asp:Literal>
        </ContentTemplate>
    </asp:UpdatePanel>
    </div>

    <div>
    <asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer2"  />
        </Triggers>

        <ContentTemplate>
            <asp:Literal runat="server" ID="txtTest2"></asp:Literal>
        </ContentTemplate>    
    </asp:UpdatePanel>
    </div>
</div>

and on code behind the ontick is the trigger, eg:

protected void Timer1_Tick(object sender, EventArgs e)
{
    txtTest1.Text += "1.";
}

Upvotes: 1

Related Questions