Introduction to the use of asp.net built-in object Response object, asp. netresponse
The Response object is an instance of the HttpRespone class. This class encapsulates HTTP information from ASP. NET operations. The Response object sends the data as the request result from the server to the client browser and provides the Response Message. It can be used to output data on the page, jump to the page, and PASS Parameters of each page.
I. Output data to the page
Syntax format
The Response object outputs data on the page through the Write or WriteFile method. The output object can be a character, String, character array, object or file.
When using Response to output data, ASP. NET's most important syntax is:Response. Write (...);
Instance:
(1) Create an ASP. NET empty website and set the webpage title to "output data on the page ".
".
(3)input the following text in the writefile.txt text file:
English can be displayed normally. for example, this is a book. <br/> but the Chinese text needs to be set: <br/> in Response. write the following statement: <br/> Response. contentEncoding = System. text. encoding. UTF8; or Response. contentEncoding = System. text. encoding. default;
(4) Find the Page_Load function in the Default. aspx. cs file and enter the following content in the function:
// Response. contentEncoding = System. text. encoding. UTF8; // Response. contentEncoding = System. text. encoding. default; char c = 'a'; string s = "print string with Response"; char [] cArray = {'Use', 'R', 'E ', 'S ', 'P', 'O', 'n','s', 'E', 'E', 'dapp', 'word', 'mapp ', 'number', 'group',}; Page p = new Page (); Response. write ("output single character:" + c + "
(5) The result of starting debugging is:
Ii. output image files to pages
1) Use the WriteFIle Method
Syntax format
This method outputs the image file to the client page as a file stream. Before using this method, you must use the ContentType attribute to define the type of file stream.
Response. ContentType = "image/JPEG ";
Response. WriteFile (including the image file name );
Instance
.
(2)find tempimage.jpg In the ephemeral hard drive file system and copy it to the Website Resource Manager. Right-click Resource Manager and select the paste option to paste the file to the project.
(3) Find the Page_Load function in the Default. aspx. cs file and enter the following content in the function:
Response.ContentType = "image/JPEG"; Response.WriteFile(@"~\tempimage.jpg");
(4) The running result is:
2) use the BinaryWrite method to output the image
Syntax format
The binary image format is output using the BInaryWrite method of the Response object as follows:
Byte [] buffer = new byte [length of an integer file];
Response. BinaryWrite (buffer );
Instance
(1) Find A *. GIF file from the Internet, save it to the ephemeral hard drive file system, and set the file name to picture.gif.
(2) Right-click website resource management and select "add existing item". The "add existing item" dialog box is displayed. Find the local location of the saved image and click "add.
(3) Find the Page_Load function in the Default. aspx. cs file and enter the following content in the function:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. IO; // added namespace public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// open the image file and the FileStream stream = new FileStream (Server. mapPath ("picture.gif"), FileMode. open); long FileSize = stream. length; // get the stream Length byte [] Buffer = new byte [(int) FileSize]; // define a binary data stream. read (Buffer, 0, (int) FileSize); // Read the byte block from the stream and write the data to the stream in the given Buffer. close (); // Close the stream Response. binaryWrite (Buffer); // output the image to the page // set the output format of the page to Response. contentType = "image/gif"; Response. end (); // other outputs on the abort page }}
(4) The running result is:
Iii. page Jump
Both the Redirect and AppendHeader methods of the Response object can implement page redirection. The Redirect method is commonly used, but this method redirects to the page, that is, the page redirection that is executed only after the page is opened. The AppendHeader method is the page redirection executed before the page is opened. The former will execute some programs on the page, while the latter will not.
1. AppendHeader Method
Syntax format
Response. AppendHeader (Name, Value)
The parameter Name is the HTTP header, and the parameter Value is the Value of the HTTP header.
The HTTP header is the content of the header domain supported by both the request and response messages specified by the HTTP protocol. The HTTP header is the first response request and response message when a page is accessed through the HTTP protocol. For example, the Location and Location header in the HTTP header is used to Redirect the page to another page, similar to the Redirect method.
Instance
(1) create a new website. The Default homepage is Default. aspx. Find the Page_Load function in the Default. aspx. cs file and enter the following content in the function:
Response.Status = "302 Object Moved"; Response.AppendHeader("Location","http://www.baidu.com");
(2) The running result is:
2. Redirect Method
Syntax format
Response. Redirect ("Redirect webpage method") Method
Instance
(1) create a new website. The Default homepage is Default. aspx. Find the Page_Load function in the Default. aspx. cs file and enter the following content in the function:
Response. Redirect (@"~ /Redirect. aspx ");
(2) Right-click the Website Resource Manager and select "Add new project". In the "Add new project" dialog box, select "Web form" and name it Redirect. aspx.
(3) In the Redirect. aspx form, add the Code:
<Form id = "form1" runat = "server"> <div> This Is A redirection page !!! </Div> </form>
(4) The running result is:
Iv. Use Response objects in combination with JavaScript
Sometimes, we need to use the C # language on the background server to execute the JavaScript code of the front-end client. One way is to use the Response object. The Response. Write () method is used to Write the JavaScript script to the 1. Pop-up dialog box
Alert is mainly used for warning in JavaScript. For example, when you want to close a webpage, you can remind the user that the webpage will be closed.
Response. Write ("<script> alert ('this is the prompt dialog box ') </script> ");
2. Open a window
Window. open is used to open a new window. The syntax format is as follows:
Response. Write ("<script> window. open (url, windowname [, loctaion]) </script> ");
You can set the document name for the new window that is opened. The window width is higher than that of other parameters.
3. Close the window
Window. close is used to close the browser window.
Response. Write ("<script> window. close () </script> ");
Instance
(1) create a new website. The Default homepage is Default. aspx. Open the design view and drag three Button controls from the toolbox to set them to Button 1, Button 2, and Button 3.
(2) double-Click the three Button controls to add the Click Event Code as follows:
Protected void button#click (object sender, EventArgs e) {Response. write ("<script> alert ('this is the prompt dialog box ') </script>");} protected void Button2_Click (object sender, EventArgs e) {// open Newwindow in a new window. aspx. The parameters are as follows: string str = "<script> window. open ('newwindow. aspx ', '', 'height = 100, width = 400, top = 0, left = 0, toolbar = no, menubar = no, scrollbars = no, resizable = no, location = no, status = no') </script> "; Response. write (str);} protected void Button3_Click (object sender, EventArgs e) {Response. write ("<script> window. close () </script> ");}
(3) Right-click "Add new Project" in Website Resource Manager, select "Web form" in the "Add new project" dialog box, and set it to Newwindow. aspx. In
Add code to the page:
<Form id = "form1" runat = "server"> <div> This is a new form opened by window. open !!! </Div> </form>
(4) The running result is:
Click the first button:
Click the second button:
After clicking the third button, I ran the Google, Firefox, 2345 browser, and the third button without a prompt box. Finally, I opened it with IE (this is the reason for browser setting ):
The above is an example of the asp.net built-in object Response. I hope it will be more helpful for you to understand the Response object.