【问题标题】:ASP Multiple UpdatePanels with gridviews but only one updating具有网格视图但只有一个更新的 ASP 多个 UpdatePanel
【发布时间】:2018-03-02 07:55:31
【问题描述】:

我希望在单个 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 函数以及(当前已注释掉的)计时器。标签随着当前时间更新,但只显示一个网格视图。

任何帮助将不胜感激 谢谢

【问题讨论】:

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


    【解决方案1】:

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

    这是我对其进行测试并有效的示例。

    <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.";
    }
    

    【讨论】:

    • 谢谢@Aristos 我已经实现了这个并且它工作了一瞬间然后它从自动生成的javascript中抛出一个javascript错误。我找到了故障部分_endPostBack: function(n, t, i) { var r, u, f; if (this._request === t.get_webRequest() &amp;&amp; (this._processingRequest = !1, this._additionalInput = null, this._request = null), r = this._get_eventHandlerList().getHandler("endRequest"), u = !1, r &amp;&amp; (f = new Sys.WebForms.EndRequestEventArgs(n, i ? i.dataItems : {}, t), r(this, f), u = f.get_errorHandled()), n &amp;&amp; !u) throw n; }
    • 错误是:Unhandled exception at line 1, column 123034 in http://localhost:57789/bundles/MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81 0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object. 它只在计时器计时结束后发生
    • 好吧,你可以忽略我最近的 2 个 cmets,我想我已经修复了这个 bug。我有这个有点工作。我明天再看一遍。
    • 这是问题的症结所在,经过一些修改,我已经能够使其适用于我的场景。谢谢你们的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 2014-12-06
    相关资源
    最近更新 更多