ASP.NET - displaying a message during page load

I have a page that performs a long-term task (10 to 15 seconds) in the page_load method.

I have a client-side javascript code that displays the user an “animated gif page” for the user.

I can call the JavaScript method from the code to display the animated gif page, however, the long-term task hangs in the user interface so that the animated gif does not actually appear until after the completion of the long-term task, which is the exact opposite of what I want.

To test this, in my page_load method, I make a JavaScript method call to display an animated gif. Then I use Thread.Sleep (10000). What happens is that the animated gif is only shown after Thread.Sleep is complete.

Obviously I'm doing something wrong.

Any advice would be appreciated.

Thank.

Chris

The following is sample code:

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

        Response.Flush();

        Thread.Sleep(10000);
    }
+5
source share
6 answers

The reason is that the Page.Load event fires before any response has been sent to the client; therefore, any instructions for the client (for example, executing your javascript) do not occur until the client receives a response.

... Page.Load . AJAX - . , , - ( spinner) , ; ( 10-15 ), DOM .

+4

. .

  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>");
+4

, . .

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

UpdatePanel . , . ...

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

        ' run method here

    End Sub
+3

JavaScript .

jQuery. DOM :

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

jQuery , script include Microsoft CDN. , :

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

GIF JavaScript , Thread.Sleep. , GIF- 10-15 .

+1

, . ( ) , ​​. , , html, , , javascript, .

+1

, Javascript onload ASP. Page.LoadComplete - , Javascript , .

.

?

0

All Articles