How to Use Response. BufferOutput in ASP. NET

Source: Internet
Author: User

The BufferOutput attribute is used to obtain or set a value indicating whether to buffer the output and send it after processing the complete page. The default value of this attribute is true. Therefore, the output content on the page is usually sent to the client only after the page is processed. If the page has a lot of content to process, it may take a long time to see the content on the page. At this time, there are two ways to gradually display the information so that you can know where the current program is running.

 

Method 1: The default value of BufferOutput is true. In this case, you can use the Flush method and the Clear method to output information about the current buffer. This method is quite common. when processing a complex program on your page, you can immediately output some prompts.

1 protected void Page_Load (object sender, EventArgs e)
2 {
3 if (! IsPostBack)
4 {
5 ShowInfo ("medium", 100 );
6 Response. Flush ();
7 Response. Clear ();
8 Thread. Sleep (1000 );
9
10 ShowInfo ("country", 5 );
11 Response. Flush ();
12 Response. Clear ();
13 Thread. Sleep (1000 );
14}
15}
16
17 private void ShowInfo (string it, int count)
18 {
19 StringBuilder sb = new StringBuilder ();
20 for (int I = 0; I <count; I ++)
21 {
22 sb. Append (it );
23}
24 sb. Append ("<br/> ");
25 Response. Write (sb. ToString ());
26}

 

Method 2: Set the BufferOutput value to false. The buffer content is immediately sent to the client for display. This method has performance problems because no buffer output is used, but the code is concise.

 

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Response. BufferOutput = false;

ShowInfo ("medium", 100 );
Thread. Sleep (1000 );

ShowInfo ("country", 5 );
Thread. Sleep (1000 );
}
}

Private void ShowInfo (string it, int count)
{
StringBuilder sb = new StringBuilder ();
For (int I = 0; I <count; I ++)
{
Sb. Append (it );
}
Sb. Append ("<br/> ");
Response. Write (sb. ToString ());
}

 

There are two problems to use:

First, the IE browser outputs data to the client only when the buffer data is no less than 256 bytes. After testing, the FF browser does not have this problem.

Second, UFT8 encoding generally occupies three bytes (Unicode encoding is two bytes). Here, the first output of 100 Chinese characters is to ensure that there are at least 256 bytes of data.

 

Related Article

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.