ASP.net built-in object Response Object use Introduction _ Practical Tips

Source: Internet
Author: User

The response object is an instance of the Httprespone class. This class is primarily to encapsulate HTTP appropriate information from the ASP.net operation. The response object sends data from the server to the client browser as the result of the request, and provides a message about the response. It can be used to output data in the page, jump in the page, and pass the parameters of each page.
first, output data to the page
Syntax format
The response object outputs data on the page by either the Write method or the WriteFile method, and the output object can be a character, a string, an array of characters, an object, or a file.
When outputting data with response, ASP. NET most important syntax is:Response.Write (...);
Instance:
(1) Set up a asp.net web site, the title of the Web page to "output data in the page."
(2) Right-click the project name in Explorer, select "Add New Item" and select "Text File" in the dialog box that pops up to add a 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 Chinese text needs to be set:<br/> 
that is, the following statement is written at Response.Write:<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 in the function:

response.contentencoding = System.Text.Encoding.UTF8; 
response.contentencoding = System.Text.Encoding.Default; 
Char c= ' a '; 
String s= "Print strings with response"; 
Char[] CArray = {' with ', ' R ', ' e ', ' s ', ' P ', ' O ', ' n ', ' s ', ' e ', ' fight ', ' print ', ' word ', ' character ', ' number ', ' Group ',}; 
Page p = new page (); 
Response.Write ("Output single character:" +c+ " 
 

(5) The result of starting a debugging run is:

Second, output image file to the page
1) using WriteFile method
Syntax format
This method outputs the picture file to the client page as a file stream. Before using this method, you must define what type of file the file stream is by using 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 tools to draw a simple image, you can download an image from the internet, saved in the local hard disk file system, the filename 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 Resource Manager and select the Paste option to paste the file into this project.
(3) Find the Page_Load function in the Default.aspx.cs file and enter the following in the function:

Response.ContentType = "Image/jpeg"; 

(4) The results of the operation are:

2) Output image in BinaryWrite method
syntax format
       The binary image format is output by the BinaryWrite method of the Response object as follows:
       byte[] Buffer=new byte[length of the whole file];
       response.binarywrite (buffer);

Instance
(1) Find a *.gif file from the Internet and save it to the local hard disk file system, with the filename set to Picture.gif.
(2) Right-click Site resource Management, select "Add Existing Item", Pop the "Add Existing Item" dialog box, find the local location where you saved the picture, and 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;//adds a 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 byte block from the stream and writes the data to the stream in the given buffer 
 . Close ();//Turn off stream 
 response.binarywrite (Buffer);//output the picture on the page 
 //Set the output format of the page 
 Response.ContentType = "image/ GIF "; 
 Response.End ()//Abort other output of the page 
 } 
} 

(4) The results of the operation are:

Third, page jump
The redirect and Appendheader methods of the response object can realize the page redirection function. The redirect method is more common, but this method jumps on the page, which is the page redirection that is performed after the page is opened. The Appendheader method is the page redirection that is performed before the page is opened. The former will also execute some of the page's programs, while the latter will not.
1, Appendheader method
Syntax format
Response.appendheader (name,value)
The parameter name is an HTTP header, and the parameter value is the value of the HTTP header.
The HTTP header is the header field content that is supported by both the request and response messages specified by the HTTP protocol. An HTTP header is a request and response message that responds first when the 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 results of the operation are:

2, redirect method
Syntax format
Response.Redirect ("redirect Web 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 the 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 code:

<form id= "Form1" runat= "Server" > 
<div> This is a redirect page!!! </div> 
</form> 

(4) The results of the operation are:

four, Response object and JavaScript use together
Sometimes, we need to use the C # language to execute the JavaScript code of the foreground client in the background server, one way is to adopt the response object. Using the Response.Write () method to write JavaScript scripts to the 1, Pop-up prompts dialog box
Alert is primarily used in JavaScript for warning purposes, such as reminding a user that a Web page will be closed when a Web page is to 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 with the following syntax format:
Response.Write ("<script>window.open (Url,windowname[,loctaion]) </script>");
You can set the name of the document, the width of the window, and some parameters for the new window open.
3. Close the window
Window.close is used to close the browser window.
Response.Write ("<script>window.close () </script>");
Instance
(1) Create a new Web site, the default home page is default.aspx, open Design view, drag from the Toolbox three button controls, respectively, set to button 1, button 2, Button 3.
(2) Double-click the three button control, add the Click event code:

protected void Button1_Click (object sender, EventArgs e) 
{ 
 Response.Write (' This is the prompt dialog box ') </script> "); 
} 
protected void button2_click (object sender, EventArgs e) 
{ 
 //open newwindow.aspx in a new window with the following parameters set 
 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 Web 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" > 
<div> 
This is the new form that window.open opens!!! 
</div> 
</form> 

(4) The results of the operation are:
Click the first button:

Click on the second button:

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

The above is about ASP.net built-in object Response example introduction, hope to understand response object more helpful.

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.