简体   繁体   中英

ASP C# update content

I assume this is a simple question for you. Here we go:

On my .aspx page, I have a label Label1 and a button Button1 surrounded by an Update Panel.

A click on the Button invokes the code-behind method that looks as follows:

protected void Click(object sender, EventArgs e) {
    ThreadProc("Hello");
    Thread.Sleep(2000);
    ThreadProc("Hello2");
    Thread.Sleep(2000);
    ThreadProc("Hello3");
}
void ThreadProc(string info) {
    Label1.Text = info;
    // UpdatePanel1.Update();
}

What I would like to see is that the Label is updated and displayed (rendered) according to what the code does. So it should Hello , Hello2 for 2 seconds and finally Hello3 should be displayed. It appears that only the last Hello3 is rendered. Can someone explain why this happens and how I can implement my idea?

The overall task I try to accomplish is that a click on the button calls a multithreaded function that queries different pages. Whenever a worker thread returns from his mission, it is supposed to update a gridview with the newly added record. Here, the issue is the same: only after all threads have done their job, the webpart is updated.

Thanks a lot for any help!

Cheers Christoph

Because the code is running on the web server, it processes the entire method Click() before rendering the final page and sending it to the client's web browser. Therefore, you only see the final results, which has "Hello3" as the Label1.Text .

The effect that Thread.Sleep(2000) has on your code is that the HTTP response from the web server takes 4.x seconds instead of only a fraction of a second if you left it out.

However, if you want to see each update to the Label1.Text , you can do that with an asynchronous AJAX call.

Actually what the code does is the following:

  • The page is requested
  • In the server the Label is updated several times
  • The page is served with the last value of the label.

The client only get the last value since the output of this code is just static HTML

What you try to accomplish has to be coded on the client side. You could use javascript to get this kind of things done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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