ASP. NET (3)-built-in object (1)

Source: Internet
Author: User

Anyone who has learned ASP knows that ASP has five common built-in objects: Request object, Response object, Application object, Session object, and Server Object. In ASP. NET, two built-in objects are added: Cache objects and Cookie objects. The seven built-in objects and their usage are described in detail below.


I,Response object

Response object is a built-in object used to output messages to the page. This object is used to send data to the client browser. You can use this objectServer DataSent in HTML FormatClient browser.


1.Common Method 1:Write Method--- Output text to the page. The sample code is shown as follows:


<Scriptrunat = 'server "> voidPage_Load (objectsender, EventArgse) {one _ tile fill 0neuron. write ("Now l 'ed is:" + DateTime. and} I </script> used the long tc method of the object called Qing n to lose Qu Xiao Qian's system money. 'quarrel 3. B 2. Erna a trade by Bi JIU nOS unavailable time} heart! Explicit <Bu, M'. xm '"sub-Two: Qing Yue <ne day 0rUn day t = SerVer> 4O blog. Advise Nebl NLP to buy online F, daily, Reslq... I'm a member of the psychology community" Swear to Xi ya ">

2.Common Method 2: Redirect--- Implement page Jump. The sample code is shown as follows:

(1) click the button to trigger the button#click event

The Code is as follows:

    

(2) Use the Response. Redirect method to Redirect pages. The Code is as follows:

<script  runat ="server"  >    protected void Button1_Click(object sender,EventArgs e)    {        Response.Redirect("Main.aspx");    }</script>

(3) page Jump to display the page:

II,Request object

A Request object is a built-in object used to extract user input information from a browser. When using this built-in object, user information can be provided through form submission, URL parameters, and environment variables.

1.Common attribute 1: QueryStringAttribute--- Accept page parameters. The reference code is as follows:

<Script runat = "server"> void Page_Load (object sender, EventArgs e) {// use the QueryString attribute of the Request object to accept parameters transmitted on the page, and determine whether the parameter is valid if (Request. queryString ["s"]! = Null & Request. QueryString ["s"]. ToString ()! = "") {// If the Request. queryString ["s"] is not null and its value is not empty. Then, assign the value to label1 control Label1.Text = Request. queryString ["s"]. toString ();} else {// otherwise, the "Accept parameter error!" Label1.Text = "the acceptance parameter is incorrect! ";}}</Script>

2.Common attribute 2: ServerVariablesAttribute--- Obtain the value of the environment variable "PATH_INFO.

The Code is as follows:

<script  runat ="server"  >    void Page_Load(object sender, EventArgs e)    {        string ServerVariable=Request.ServerVariables ["PATH_INFO"];        Response.Write(ServerVariable);    }</script>

Page display:

Vc3Q6MzI0MDZsTG9naW6hpGFzcHi5 + r + release/zc34ubrQxMDt0afB9M28L0xvZ2luLmFzcHg = ">

Note:After a general understanding of the two, we can compare and find that the Response object and the Request object constitute a pair of objects for receiving and sending data. As shown in:


III,Application Object

The Application object is used to share information among all users and maintain data permanently during the running of the program. For example, we can store the current number of online accesses to a website in this object.

First, we need to initialize the global variable "user_count" on the initialization page and assign the value to 1. Then we need to access the Page code:

<Script runat = "server"> void Page_Load (object sender, EventArgs e) {// obtain the global variable value and display the current number of online users. write ("current number of online users:" + Application ["user_count"]); Application ["user_count"] = Convert. toInt32 (Application ["user_count"]) + 1 ;}</script>

To simplify the length of the article, we will first introduce the above three built-in objects:Request object, Response object, and Application object. The remaining four Session objects, Server objects, Cache objects, and Cookie objects will be introduced in the next blog ~

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.