【问题标题】:Enable Timer after the page is loaded页面加载后启用定时器
【发布时间】:2025-12-17 17:05:02
【问题描述】:

我有一个 Web 应用程序并使用 UpdatePanel 和 Timer 来刷新 GridView 数据。我将 Timer 间隔设置为 10000。当页面加载时,显示来自数据库服务器的 GridView 数据需要 10000 毫秒。如何在页面加载时禁用计时器并在页面加载后启用它?换句话说,数据应该在页面加载后立即显示。我很感激任何指点。

<asp:UpdatePanel ID="DisplayResultsUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
         <div>
            <asp:GridView ID="DisplayResultsGridView" AllowSorting="true" gridlines="Vertical" OnSorting="DisplayResultsGridView_Sorting" onrowdatabound="DisplayResultsGridView_RowDataBound" runat="server" > 
            </asp:GridView>   
        </div>  

         <asp:Timer ID="DisplayResultsTimer" Interval="10000" Enabled="true" runat="server" OnTick="DisplayResultsTimer_Tick">    
            </asp:Timer>  
    </ContentTemplate>
</asp:UpdatePanel>

protected void DisplayResultsTimer_Tick(object sender, EventArgs e)
{
    PageRefreshAsOf.Text = DateTime.Now.ToLongTimeString();
}

【问题讨论】:

    标签: c# gridview timer updatepanel


    【解决方案1】:

    您可以尝试实现 loadcomplete 事件,如下所述: http://msdn.microsoft.com/en-us/library/system.web.ui.page.loadcomplete.ASPX

    <asp:Page OnLoadComplete="EventHandler" />
    

    有关事件的更多文档,请参阅: http://msdn.microsoft.com/en-us/library/ms178472.ASPX

    【讨论】:

    • 感谢您的指点。我终于找到了问题所在。我将错误的参数传递给数据访问方法。这就是页面加载后没有立即显示数据的原因。