ASP. NET built-in object response Object usage Introduction

Source: Internet
Author: User

The response object is an instance of the Httprespone class. This class is primarily encapsulated with HTTP appropriate information from an ASP. The response object sends the data as the result of the request from the server to the customer's browser, and provides a message about the response. It can be used to output data in a page, jump on a page, and pass parameters to individual pages.

First, output data to the page

The response object outputs data through the Write method or the WriteFile method on the page, and the output object can be a character, string, character array, object, or file.

When outputting data with response, ASP. NET the most important syntax is:Response.Write (...);

Output image files to the page

1) using the WriteFile method
Syntax format
This method outputs the picture file to the client page as a file stream. Before the method is used, the file stream must be defined by the ContentType property.
response.contenttype= "Image/jpeg";
Response.WriteFile (contains the file name of the picture);

2) Output image with BinaryWrite method
Syntax format
The output binary image format is as follows through the BinaryWrite method of the Response object:
byte[] buffer=new byte[integer file length];
Response.BinaryWrite (buffer);

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.IO;//added Namespaces     Public Partial class_default:system.web.ui.page {protected voidPage_Load (Objectsender, EventArgs e) {  //Open the picture file and exist in the file streamFileStream stream =NewFileStream (Server.MapPath ("Picture.gif"), FileMode.Open); LongFileSize = stream. Length;//gets the length of the stream byte[] buffer=New byte[(int) FileSize];//define a binary dataStream. Read (Buffer,0,(int) FileSize);//reads a block of bytes from the stream and writes the data to the given bufferStream. Close ();//Close the streamResponse.BinaryWrite (Buffer);//output a picture on a page//set the output format of the pageResponse.ContentType ="Image/gif"; Response.End ();//Abort other output of the page } } 

Third, page jump
The redirect and Appendheader methods of the response object can be used to implement the page redirection function. The redirect method is more commonly used, but the method jumps on the page, which is the page redirection that is performed only after the page is opened. The Appendheader method is a page redirection that is performed before the page is opened. The former also executes some of the page's programs, while the latter does not.
1. Appendheader method
Syntax format
Response.appendheader (Name,value)
The parameter name is the HTTP header, and the parameter value is the HTTP header.
The HTTP header is the header domain content that is supported by both the request and response messages specified by the HTTP protocol. HTTP headers are the first response to a request and response message when a page is accessed through the HTTP protocol, such as the location,location header in the HTTP header used to redirect the page to another page, similar to the redirect method.

" 302 Object Moved " ; Response.appendheader ("location","http://www.baidu.com" 

2. Redirect Method
Syntax format
Response.Redirect ("redirect Web page Method") method

Iv. response objects used in conjunction with JavaScript
Sometimes we need to execute the JavaScript code of the foreground client in the background server through the C # language, one way is to use the response object. Using the Response.Write () method to write JavaScript scripts to the 1. Popup Prompt dialog box
Alert is used primarily as a warning in JavaScript, such as when a webpage is about to be closed, reminding the user that the page will be closed.
Response.Write ("<script>alert (' This is the prompt dialog box ') </script>");
2. Open the Window
Window.Open is used to open a new window in the following syntax format:
Response.Write ("<script>window.open (Url,windowname[,loctaion]) </script>");
You can set the name of the document to a new window that opens, and some parameters for the width of the window.
3. Close the window
Window.close is used to close the browser window.
Response.Write ("<script>window.close () </script>");

Wu, Response.Buffer

The Buffer property indicates whether the page output is buffered. When a buffered page is output, the server sends the response to the client only after all server scripts for the current page have been processed or the Flush or End method is called.

The Buffer property can no longer be set after the server has been sent to the client. Therefore, you should call Response.Buffer on the first line of the. asp file.

Grammar
[=flag]
Parameters
Flag
Specifies whether to buffer the page output, which can be one of the following values.
Value Description
FALSE Not buffered. The value is the default value. The server sends the output to the client while processing the script.
TRUE The server does not send a response to the client unless all ASP scripts for the current page have finished processing or called the flush or End method.
Comments

If the current ASP script buffer is set to TRUE, but the Flush method is not called, the server keeps the client's request active. Because the server does not have to create a new connection for each client, it saves time.

However, Buffering will prevent the client from displaying a response before the server has finished processing all the scripts on the current page. For long scripts, it is possible to feel a delay.


That is, when buffer is true, ASP will send the program to the client after the interpretation is complete.
When buffer is false, ASP sends data on edge parsing edge.

ASP. NET built-in object response Object usage Introduction

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.