简体   繁体   English

ASP.NET - 页面加载时显示消息

[英]ASP.NET - Display Message While Page Is Loading

I have a page that performs a long-running task (10 to 15 seconds) in the page_load method. 我有一个页面在page_load方法中执行长时间运行的任务(10到15秒)。

I have client-side javascript code that will display a decent "page loading" animated gif to the user. 我有客户端的JavaScript代码,将向用户显示一个体面的“页面加载”动画GIF。

I am able to invoke the JavaScript method from the code-behind, to display the "page loading" animated gif, however, the long-running task is hanging up the UI such that the animated gif doesn't actually display until after the long-running task is complete, which is the exact opposite of what I want. 我能够从代码隐藏中调用JavaScript方法,以显示“页面加载”动画gif,但是,长时间运行的任务挂起UI,使得动画gif实际上不会显示,直到长时间之后 - 运行任务完成,这与我想要的完全相反。

To test this out, in my page_load method I make a call to the JavaScript method to display the animated gif. 为了测试这一点,在我的page_load方法中,我调用JavaScript方法来显示动画gif。 Then, I use Thread.Sleep(10000). 然后,我使用Thread.Sleep(10000)。 What happens is that the animated gif doesn't display until after Thread.Sleep is complete. 发生的事情是,直到Thread.Sleep完成后才会显示动画gif。

Obviously I am doing something incorrect. 显然我做错了什么。

Any advice would be appreciated. 任何意见,将不胜感激。

Thanks. 谢谢。

Chris 克里斯

Below is an example of the code-behind: 下面是代码隐藏的示例:

protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript
              (GetType(), "Javascript", "javascript: ShowWaitIndicator(); ", true);

        Response.Flush();

        Thread.Sleep(10000);
    }

I accomplished this by placing a timer on the page. 我通过在页面上放置一个计时器来实现这一点。 After its first tick disable it and run your task. 在第一次勾选后禁用它并运行您的任务。

<asp:Timer runat="server" id="UpdateTimer" interval="500" ontick="UpdateTimer_Tick" />

I placed this within an UpdatePanel for my needs. 我把它放在UpdatePanel中以满足我的需求。 I'm not sure what will work best for you. 我不确定哪种方法最适合你。 Then in your code behind... 然后在你的代码背后......

Protected Sub UpdateTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        UpdateTimer.Enabled = False

        ' run method here

    End Sub

The reason is that the Page.Load event fires before any response has been sent to the client; 原因是Page.Load事件任何响应发送到客户端之前触发; so any instructions for the client (such as executing your javascript) doesn't occur until the client receives the response. 因此,在客户端收到响应之前,不会发生客户端的任何指示(例如执行javascript)。

So... placing the long-running task in Page.Load won't have the effect you want. 所以......在Page.Load中放置长时间运行的任务将无法获得您想要的效果。 This sounds like a case for using AJAX or some other form of asynchronous data-retrieval. 这听起来像是使用AJAX或其他形式的异步数据检索的情况。 In this scenario the page you return to the client doesn't containt he result of your long-running task--so the page itself (with it's spinner) loads quickly, then the client requests the data; 在这种情况下,返回到客户端的页面不包含长期运行任务的结果 - 因此页面本身(使用它的微调器)加载很快,然后客户端请求数据; when the data is ready (10-15 seconds later) you can update the DOM in the client with the results. 当数据准备就绪(10-15秒后),您可以使用结果更新客户端中的DOM。

Following example will work for you. 以下示例将适合您。 just place in Page Load Event. 只是放在页面加载事件中。

  Response.Write("<div id=\"loading\" style=\"position:absolute; width:100%; text-align:center; top:300px;\"><img src=\"http://www.cse.iitk.ac.in/users/singhroh/images/loading.gif\" border=0></div>");
  Response.Flush();
  System.Threading.Thread.Sleep(5000);        
  Response.Write("script>document.getElementById('loading').style.display='none';</script>");

I would avoid invoking your JavaScript from the code-behind. 我会避免从代码隐藏中调用你的JavaScript。

Instead, utilize the jQuery library. 相反,使用jQuery库。 You can trigger your code to be called immediately after the DOM is loaded by using the following code: 您可以使用以下代码触发在加载DOM后立即调用的代码:

$(document).ready(function() {
     //Call your JavaScript method here.
});

You'll also need to add jQuery to your page, which is a single script include from the Microsoft CDN. 您还需要将jQuery添加到您的页面,这是Microsoft CDN中包含的单个脚本。 Add this to your markup to do this: 将其添加到您的标记中以执行此操作:

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>

This will call your GIF JavaScript method immediately when the page is loaded and you can eliminate your Thread.Sleep. 这将在页面加载时立即调用您的GIF JavaScript方法,您可以消除Thread.Sleep。 I'm assuming your animated GIF method automatically hides the image after 10-15 seconds. 我假设您的动画GIF方法会在10-15秒后自动隐藏图像。

Taking it old school, you could switch buffering off. 把它放到旧学校,你可以切换缓冲。 With buffering on (the default), the page is generated in it's entirity before it's sent to the client. 启用缓冲(默认设置)后,页面会在发送到客户端之前以其完整性生成。 If you switch buffering off, it's sent as it's loaded, so you can send the html to switch the loading graphic on, do your long running task, then send some javascript to switch it off. 如果您关闭缓冲,它会在加载时发送,因此您可以发送html来切换加载图形,执行长时间运行的任务,然后发送一些javascript来关闭它。

It sounds like the Javascript onload is being displayed after the ASP onload processes. 听起来像ASP onload进程后显示Javascript onload。 You may want to delay your time-intensive action to Page.LoadComplete - this way, your user will see the Javascript render before the intensive operation occurs. 您可能希望将时间密集型操作延迟到Page.LoadComplete - 这样,您的用户将在密集型操作发生之前看到Javascript渲染。

Then, you would update the page contents as needed after the function completes. 然后,您将在函数完成后根据需要更新页面内容。

Can you provide an example of the code? 你能提供一个代码示例吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM