Dynamic website development technology learning 3: ASP. NET built-in object Response object

Source: Internet
Author: User

Dynamic website development technology learning 3: ASP. NET built-in object Response object

The Response object is an instance of the HttpRespone class. This class encapsulates HTTP information from ASP. NET operations. Response

The 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

Page jump, you can also pass parameters for each page.

Common attributes, methods, and descriptions of Response objects:

Always output data on 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

Image 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 ".

(2) Right-click the project name in resource manager and select the "Add new project" command. In the displayed dialog box, select "Text File ".

Set it to writable writefile.txt ".

(3)input the following text in the writefile.txt text file:

English can be displayed normally. for example, this is a book. however, you need to set the Chinese text: in Response. write the following statement: 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 the string with Response; char [] cArray = {'Use', 'R', 'E','s ', 'P', 'O', 'n','s ', 'E', 'E', 'dapp', 'input', 'word', 'mapp', 'number ', 'group',}; Page p = new Page (); Response. write (output single character: + c +
); Response. Write (output a string: + s + ); Response. Write (output character array :); Response. Write (cArray, 0, cArray. Length); Response. Write ( ); Response. Write (output an object: + p + ); Response. Write (output a file: + ); Response. WriteFile (@~ WriteFile.txt );

 

(5) The result of starting debugging is:

Two-way output of image files

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 definition text

File type.

Response. ContentType = image/JPEG;

Response. WriteFile (including the image file name );

Instance

(1) We can use the built-in drawing tool of Windows to easily draw an image, or download an image from the Internet and save it on a local hard drive.

In the system, set the file name to tempimage.jpg.

(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 Paste.

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(@~empimage.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. Locate the local location of the saved image and click "add ".

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) and FileMode exist in the file stream. 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:

3 page Jump

Both the Redirect and AppendHeader methods of the Response object can implement page redirection. Redirect method is commonly used, but this method is

Page jump, 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. Before

But the latter will not.

1AppendHeader 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 most

The first response request and Response Message, such as the Location and Location header in the HTTP header, is used to Redirect the page to another page.

Similar.

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

As follows:

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

(2) The running result is:

2Redirect Method

Syntax format

Response. Redirect (webpage redirection 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

As follows:

    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:
       

(4) The running result is:

4. 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 Response

Object. Use the Response. Write () method to Write JavaScript scripts to

And execute.

Three methods:

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, drag three Button controls from the toolbox, and set them

Button 1, button 2, 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:

    

(4) The running result is:

Click the first button:

Click the second button:

Click the third button. I have run Google, Firefox, and 2345 browsers. The third button does not show a prompt box. I finally opened it with IE.

Effect (this is the reason for browser setting ):

 

 

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.