ASP.NET AJAX: Firing an UpdatePanel after the page load is complete

后端 未结 8 2404
情深已故
情深已故 2021-02-15 07:35

I\'m sure this is easy but I can\'t figure it out:

I have an ASP.NET page with some UpdatePanels on it. I want the page to completely load with some \'Please w

相关标签:
8条回答
  • 2021-02-15 07:54

    A thumbs-up and thanks to SAL and the rest of you guys. This solved a big issue I had, my procedure was taking up to a minute for the page to finally render and display.

    Thanks!

    0 讨论(0)
  • 2021-02-15 07:59

    I fiddled around with the ScriptManager suggestions - which I reckon I would have eventually got working but it seems to me that the Timer idea is easier to implement and not really(!) that much of a hack?!

    Here's how I got my panel updated after the initial page render was complete...

    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>
    

    and the code behind default.aspx.cs reads

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

    So, the page loads up and the Timer (contained within the UpdatePanel) fires 2 secs after the page has loaded up (I think - I'm not sure when the Timer actually starts?). The label text is rewritten and then the Timer is disabled to stop any more updates.

    Simple enough - but can you purists out there tell me if this is a Horrible Hack?

    0 讨论(0)
  • 2021-02-15 08:08

    Using Tom's approach with the startup script, you can then call:

    __doPostBack('UpdatePanelName', '');

    0 讨论(0)
  • 2021-02-15 08:09

    Use a timer control that will be fired after a certain number of milliseconds (for page to load). In the timer tick event refresh the update panel.

    0 讨论(0)
  • 2021-02-15 08:10

    The ScriptManager.RegisterStartupScript allows a script to run on startup inside of an update panel. if you use the old ClientScript.RegisterStartupScript then the script you render will be outside the bounds of the udpate panel, and thus won't be executed during async page loads.

    0 讨论(0)
  • 2021-02-15 08:11

    Have a look at ScriptManager.RegisterStartupScript

    The idea is that you register a script to run on start up (I believe once the page has loaded). Your script should call a function that causes a post back through your UpdatePanel

    0 讨论(0)
提交回复
热议问题