Summarize the ASP. NET built-in object (Response) Usage Method instance

Source: Internet
Author: User
This article mainly introduces the ASP. NET built-in objects: Response object Use introduction, the response object interested in the small partners can refer to

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
Syntax format
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 (...);
Instance:
(1) Create an ASP. NET site, set the title of the page to "output data in the page."
(2) Right-click on the project name in Explorer, select "Add New Item" command, select "Text File" in the dialog box for adding new item, and the name is set to "WriteFile.txt".
(3) Enter the following text in the WriteFile.txt text file:

中文版 can be displayed normally. For Example,this is a book.<br/> but the Chinese text needs to be set up:<br/> that is to write the following statement in Response.Write:<br/> response.contentencoding = System.Text.Encoding.UTF8; or response.contentencoding = System.Text.Encoding.Default; 

(4) Locate the Page_Load function in the Default.aspx.cs file and enter the following 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 = {' with ', ' R ', ' e ', ' s ', ' P ', ' O ', ' n ', ' s ', ' e ', ' hit ', ' print ', ' word ', ' character ', ' number ', ' Group ',}; Page p = new page (); Response.Write ("Output single character:" +c+ "

(5) The result of starting the debugging operation is:

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);
Instance
(1) We can use the Windows system with the drawing tool to easily draw an image, you can download an image from the internet, saved in the local hard disk file system, the file name is set to Tempimage.jpg.
(2) Locate the tempimage.jpg in the local hard disk file system and copy it to the Web site Explorer. Right-click the Explorer and select the Paste option to paste the file into this project.
(3) Locate the Page_Load function in the Default.aspx.cs file and enter the following in the function:

Response.ContentType = "Image/jpeg"; Response.WriteFile (@ "~\tempimage.jpg");

(4) The result of the operation is:

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

Instance
(1) Find a *.gif file from the Internet, save it to the local hard disk file system, and set the file name to Picture.gif.
(2) Right click on the site resource management, select "Add Existing Item", Pop up the "Add Existing Item" dialog box, find the local location where you saved the picture, click "Add".
(3) Locate the Page_Load function in the Default.aspx.cs file and enter the following 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 picture file and exist in file stream  FileStream stream = new FileStream (Server.MapPath ("Picture.gif") , FileMode.Open);  Long FileSize = stream. length;//gets the length of the stream  byte[] buffer=new byte[(int) filesize];//defines a binary data  stream. Read (buffer,0, (int) FileSize);//reads a block of bytes from the stream and writes that data to the stream in the given buffer  . Close ();//Turn off stream  response.binarywrite (Buffer)//Output picture on page  //Set Output format of page  Response.ContentType = "image/ GIF ";  Response.End ();//Abort other output of the page  }}

(4) The result of the operation is:

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.
Instance
(1) Set up a new website, the default homepage is default.aspx. Locate the Page_Load function in the Default.aspx.cs file and enter the following in the function:


Response.Status = "302 Object Moved"; Response.appendheader ("Location", "http://www.baidu.com");

(2) The result of the operation is:

2. Redirect Method
Syntax format
Response.Redirect ("redirect Web page Method") method
Instance
(1) Set up a new website, the default homepage is default.aspx. Locate the Page_Load function in the Default.aspx.cs file and enter the following in the function:
Response.Redirect (@ "~/redirect.aspx");
(2) Right-click Site Explorer, select Add New Item, and in the Add New Item dialog box, select Web Form, named Redirect.aspx.
(3) In the Redirect.aspx form, add the code:


<form id= "Form1" runat= "Server" > <p> this is the redirect page!!! </p> </form>

(4) The result of the operation is:

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>");
Instance
(1) Create a new website, the default home page is default.aspx, open the Design view, drag the three button controls from the Toolbox, set to Button 1, button 2, button 3, respectively.
(2) Double-click the three button control, and add the code for the Click event:

protected void Button1_Click (object sender, EventArgs e) {  Response.Write ("<script>alert (' This is a hint dialog box ') </ Script> "); } protected void Button2_Click (object sender, EventArgs e) {  //opens newwindow.aspx in a new window, each parameter is set as  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 item in the site Explorer, and in the Add New Item dialog box, select Web Form, set to newwindow.aspx. In
Add code to the page:


<form id= "Form1" runat= "Server" > <p> this is the new form of window.open Open!!! </p> </form>

(4) The result of the operation is:
Click the first button:

Click the second button:

Click the third button, I ran the Google,firefox,2345 browser, the third button did not pop up the box, and finally use IE opened with the effect (this is the reason for browser settings):

The above is an introduction to an example of ASP. NET built-in object response, which I hope will help you understand response objects.

"Recommended"

1. SPRINGMVC implements JSON interaction-requestbody and responsebody (graphic)

2. Angularjs's NG Http request and Response format conversion method

3. Java parsing XML in response

4. Response Object tutorial for sharing ASP.

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.