Use Response.Write to achieve front and back interaction in the life cycle of a page

Source: Internet
Author: User

The Response.Write () method is very common, and is common, which is to output a string to HTTP outputs. Its output is located at the top of the page, commonly used to display some page message boxes and other logic.

In general, the content that is output through Response.Write during the entire life cycle of the page will be output to the client side at the end of the document flow with the page. But with the help of Response.Flush, we can realize the dynamic interaction of the front and rear stations in the process.

The Response.Flush method forces the content in the buffer to be output to the client side without waiting for the end of the life cycle. Through an example to deepen understanding, the instance in real-time response to the progress of the background method execution.

Each time the background attempts to update the client progress, will be output by Response.Write A JS script, through JS to update the progress value.

 for (int1; i++) {    thread.sleep (+);    Setprocessvalue (i);}

Private void setprocessvalue (int  percent) {    new  StringBuilder ();    Sb. Append ("<script>setprocessbarvalue ('" ") </script >");    HttpContext.Current.Response.Write (sb.) ToString ());    HttpContext.Current.Response.Flush ();}

  

Running the code, you will see that progress value is incremented incrementally as the method executes. The Response.Flush call ensures that the content is output to the client in a timely manner whenever execution occurs.

Remove all Response.Flush from the code and run the code. The result changes, and progress value is changed from 0 to 100 only once the page has been loaded completely. Sample code

  

Use Response.Write to achieve front and back interaction in the life cycle of a page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.