开发者

Updating on-screen element several times in one function

This has got to be easy, but has got me stumped.

How do I change an element on screen several times in one function?

I want to change a label to read something like "starting..." at the beginning of my function, then go away and do some stuff and then change the label to "finished" at the end.

As an illustration why does the following code:

        errorMessage.Text = "Three...";
        System.Threading.Thread.Sleep(1000);
        errorMessage.Text = "Two...";
        System.Threading.Thread.Sleep(1000);
        errorMessage.Text = "One...";
        System.Threading.Thread.Sleep(1000);
        errorMessage.Text = "Go!";
        System.Threading.Thread.Sleep(1000);

Just pause for 4 seconds and then change the label text to "Go开发者_高级运维!" rather than counting down?

TIA

Ben


If you want to time a process and put your results out to a label you should use a string in your function instead. Try something like this:

string countDownTimes = "";
countDownTimes += String.Format("One at: {0}, ",DateTime.Now.ToString());
System.Threading.Thread.Sleep(1000); 
countDownTimes += String.Format("Two at: {0}, ",DateTime.Now.ToString());
//etc..
errorMessage.Text = countDownTimes;

If your looking to update the UI button in increments then you should look at a javascript solution or async updates.


The reason that your code only updates the screen once is that it is running inside of a single postback. At the end of the postback, HTML is rendered in returned to the browser for display.

Updating the page multiple times during a postback actually isn't easy. But for what you're doing, there is an easier solution. Use JavaScript to change the caption to "Starting..." at the beginning of the request, and then have the method return "Finished."


Because it is a web page, and the final value is what is sent to the browser. All server side code executes and then sends the final values to browser.

Your code is updating the control with the new value on the server, but it won't be seen by the client. To do what you want, you either have to update the control through client side script (javascript), or you have to refresh the page and display the updated value.

Here is a link to the asp.net pages life cycle which is kinda what you are asking about.

http://msdn.microsoft.com/en-us/library/ms178472.aspx

If you notice nothing is sent to the browser until the Render method.


You need to learn about how the web works;

  1. A browser requests a page from the server
  2. The server sends the page (and might do some server-side processing such as ASP.net)
  3. The client shows the page and runs javascripts on the page

Then these javascripts might change the page after it has loaded, but step 2 can't go and change something that's already been sent to the client.


If it's ASP.NET all the code will be executed prior to send the html to the browser so only the last value will be shown. If you want to do that at client side you will have to do some javascript.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜