ASP.NET AJAX:在页面加载完成后触发UpdatePanel

SAL*_*SAL 7 javascript asp.net asp.net-ajax

我确信这很容易,但我无法弄清楚:

我有一个ASP.NET页面,上面有一些UpdatePanels.我希望页面完全加载UpdatePanels中的一些"请稍候"文本.然后,一旦页面完全加载,我想调用代码隐藏函数来更新UpdatePanel.

关于Javascript和代码隐藏的哪些组合我需要实现这个想法的任何想法?

SAL

PS:我已经尝试将我的函数调用放在Page_Load中,但是在页面交付之前运行代码,并且由于我想运行的函数需要一些时间,因此页面加载时间太长.

SAL*_*SAL 8

我摆弄了ScriptManager的建议 - 我认为我最终会得到它的工作,但在我看来,Timer的想法更容易实现,而不是真的(!)那么多的黑客攻击?!

这是我在初始页面渲染完成更新我的面板的方法......

Default.aspx的

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAXPostLoadCall._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2>And now for a magic trick...</h2>
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick" />
                    <asp:Label ID="Label1" runat="server">Something magic is about to happen...</asp:Label>
                </ContentTemplate>
            </asp:UpdatePanel>

        </div>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

和default.aspx.cs背后的代码读取

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace AJAXPostLoadCall
{
    public partial class _Default : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void DoMagic()
        {
            Label1.Text = "Abracadabra";
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            // Do the magic, then disable the timer
            DoMagic();
            Timer1.Enabled = false;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

因此,页面加载并且页面加载后定时器(包含在UpdatePanel中)会在2秒后触发(我想 - 我不确定Timer何时实际启动?).重写标签文本,然后禁用Timer以停止更多更新.

很简单 - 但是你可以告诉我这是一个可怕的黑客吗?


小智 1

使用计时器控件,该控件将在一定毫秒数(用于加载页面)后触发。在计时器滴答事件中刷新更新面板。