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.