ASP具有gridviews的多个UpdatePanel但只有一个更新

时间:2018-03-02 07:55:32

标签: c# asp.net asp.net-ajax

我希望在单个数据库查询(显示静态数据,无操作)的单个aspx页面上有3个网格视图,并基于10秒计时器刷新表格。我有代码返回已排序的数据表。我可以让它更新一个gridview,它位于我的一个更新面板中,但其他两个不会渲染。

代码是:

<%@ 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>

我无法弄清楚为什么我的某个更新面板正在运行。正如您所看到的,我已尝试使用PreRender功能以及(目前已注释掉的)计时器。标签正在使用当前时间进行更新,但只显示一个gridview。

任何帮助将不胜感激 感谢

1 个答案:

答案 0 :(得分:1)

这里的问题是,在UpdatePanel内部回发后,计时器的脚本丢失了 - 解决方案将其从更新面板中取出并使用触发器

以下是我测试并运行的示例。

<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>

并且ontick后面的代码是触发器,例如:

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