URL, Session, Cookies, Server. Transfer, Application, and cross-page Transfer.

Source: Internet
Author: User

URL, Session, Cookies, Server. Transfer, Application, and cross-page Transfer.
[Analysis]
This topic examines whether the interviewer's understanding of Multi-page pass-through in ASP. NET is comprehensive. Because the ASP. NET page form is submitted to itself to complete the return function, by default, the POST method cannot be used for multi-page value passing. The analysis of these value passing methods is as follows.
1. URL-based value transfer
This is a classic value passing method. This method is very simple to use, but the passed value is displayed in the address bar of the browser, and the object cannot be passed. Therefore, this method is generally used when the passed value is small and the security requirements are not high. In the *. aspx page development, you can use hyperlink text to transfer values, as shown in the following code.
<Asp: HyperLink runat = "server" ID = "maid"
NavigateUrl = "~ /GetValues. aspx? Urlvalue1 = intel & urlvalue2 = amd "> URL-based value transfer </asp: HyperLink>
Go to the GetValues. aspx page with the link text, and pass the urlvalue1 variable and urlvalue2 variable through URL. The values are intel and amd respectively. In the *. cs server method, you can also call the Redirect method of the HttpResponse object to Redirect the browser to a new page and pass the value through the URL, as shown in the following code.
Response. Redirect ("~ /GetValues. aspx? Urlvalue1 = intel & urlvalue2 = amd ");
2. Pass the Session Value
This method stores each piece of data in server variables and can transmit a large amount of data, which is highly secure. Therefore, it is often used for user identity verification. However, if the Session variable stores too much data, it will consume too much server resources. programmers should be careful when using it. The usage method is shown in the following code.
Session ["SessionValue"] = "ATI ";
// You can also use index Storage
Session [0] = "ATI ";
// The valid value of the server Method on other pages is as follows:
String str = Session ["SessionValue"]. ToString ();
// You can also use the index to obtain
String str = Session [0]. ToString ();
Session can be shared among multiple pages of the application by name/value pairs until the browsing user closes his browser or server Session times out (configurable, the default value is 20 minutes ).
3. Pass Cookie values
Cookie is a special data storage method, because it stores data in the browser's computer and exists in the disk as a text file. This method is very interesting. Many login systems use cookies to automatically log on to users. That is, the user's login information will be written to the Cookie file on the user's computer. The website will automatically read the Cookie to complete identity verification at the next login. Although it is convenient to transmit data through cookies, the storage time can be set freely, but the security is not high, programmers should not rely too much on cookies, but should use a combination of methods to store sensitive data.
The main types of Cookie operations in ASP. NET are located in the System. Web namespace, including HttpCookie, HttpResponse, and HttpRequest. HttpCookie can be used to establish and operate the Security type of an independent Cookie. You can access the Cookies of HttpResponse and HttpRequest to obtain the HttpCookieCollection object to read or add an HttpCookie object. The method for adding a Cookie is shown in the following code.
// Create an HttpCookie object and pass the name value to the constructor
HttpCookie ck = new HttpCookie ("CookieValue ");
// Set the "Value" attribute (Value) of ck)
Ck. Value = "Cookie Value ";
// Set the expiration time of the ck to the current time Plus 360 s, that is, 6 minutes later the expiration time expires.
Ck. Expires = DateTime. Now. AddSeconds (360 );
// Set the effective range of ck (Domain Restriction)
Ck. Domain = "www.google.cn ";
// Add the ck to the HttpCookieCollection object
Response. Cookies. Add (ck );
// You can also add
// Response. AppendCookie (ck );;
Reading a user's Cookie is simpler. You only need to read the subitem in the HttpCookieCollection object according to the name, as shown in the following code.
String str = Request. Cookies ["CookieValue"]. Value;
In addition, you can use an HttpCookie to store Cookie information in the form of multiple name/value pairs, as shown in the following code.
HttpCookie ck = new HttpCookie ("CookieValue ");
// Add multiple Cookie information by name/value pair
Ck. Values. Add ("value1", "onE ");
Ck. Values. Add ("value2", "two ");
// Set the expiration time of the ck to the current time plus the date of January 1, 100, that is, the expiration time after January 1, 100.
Ck. Expires = DateTime. Now. AddYears (100 );
// Use either of the following methods to add
Response. Cookies. Add (ck );
// Response. AppendCookie (ck );
The following code reads these cookies.
String str = "value 1:" + Request. Cookies ["CookieValue"]. Values ["value1"];
Str + = "value 2:" + Request. Cookies ["CookieValue"]. Values ["value2"];
Note: Most browsers support cookies of up to 4096 bytes. If you want to save a few values to your computer, the browser also limits the number of cookies that each site can save on your computer. Most browsers only allow 20 cookies to be saved on each site. If you try to save more cookies, the first Cookie will be deleted. Some browser may limit the total number of cookies from all sites. This limit is usually 300. Users can set their own browsers to reject cookies. In this case, they can only use other data storage methods.
4. Server. Transfer
There are many steps for this method. programmers can use this method to access values by exposing Object Attributes on another page. This method is object-oriented. The code of this method is not complex. First, you can define a public permission attribute to return the value to be passed. Then, on the second page, use the Context. Handler attribute to obtain the reference of the instance object on the previous page. Then, you can access the custom attribute to obtain the required value.
Suppose SendValues. the aspx page is the data transmission page in SendValues. aspx. cs defines a public attribute in the page class and returns the value to be passed through get (this can be the attribute value of a server control on the page ).
Public string TransferData
{
Get
{
Return "";
}
}
Call the Transfer method of the HttpServerUtility object in a method of SendValues. aspx. cs (such as the Click Event Processing Method of the Button control), as shown in the following code.
Server. Transfer ("~ /GetValues. aspx ");
Assume that GetValues. aspx is the second page and the value passed on the previous page is received in the Page_Load method of GetValues. aspx. cs, as shown in the following code.
// Defines the variable SV_Page of the SendValues type
SendValues SV_Page;
// Access the Handler attribute of the HttpContext object, convert the value to the SendValues type, and assign the reference value to SV_Page.
SV_Page = (SendValues) Context. Handler;
// SV_Page is the object of the SendValues. aspx page class, And the TransferData attribute value is obtained directly.
String str = SV_Page.TransferData;
As you can see, this method is very easy to understand for programmers with object-oriented basics. However, if you need to pass a large number of values between pages, This method requires too many attributes to be defined, which is a little complicated. Programmers can also directly use the "Iterms" attribute of the HttpContext object to add data with multiple name/value pairs in SendValues. aspx. in a method that cs calls the Transfer method of the HttpServerUtility object, modify the code as follows.
Context. Items ["value1"] = "onE ";
Context. Items ["value2"] = "two ";
In the Page_Load method of GetValues. aspx. cs, the value passed on the previous page is received. The modification code is shown in the following code.
String str = "value 1:" + Context. Items ["value1"]. ToString ();
Str + = "value 2:" + Context. Items ["value2"]. ToString ();
5. Pass the Application value
Strictly speaking, the HTTP application object generates a status variable on the server to store the required information. The availability of the HttpApplication object variable covers the entire WEB application. Therefore, this object generally stores information to be published, such as the number of online users. This method is not used for sensitive data involving individual users. The HttpApplication object has two common methods: Lock and UnLock, which can be used to write data stored in the Application variable by multiple users. The Lock method locks all Application variables to prevent other users from modifying the variable values of the Application object. The UnLock method unlocks the variables of the HttpApplication object. The method for passing values through the HttpApplication object is similar to the Session method. Write the code in a method of the page class as follows.
Application ["a"] = "Microsoft ";
Application ["B"] = "apple ";
Application ["c"] = "Mac ";
The above code easily saves the three string values in the variables of the HttpApplication object, and these variables are shared by the entire program, which can be obtained by other users through the corresponding page. The following code retrieves these variables.
Application. Lock ();
String str = "value 1:" + Application ["a"];
Str + = "value 2:" + Application ["B"];
Str + = "value 3:" + Application ["c"];
Application. UnLock ();
Note: although the usage is similar to the Session method, the Session is for each individual user. When the user closes the browser, the Session becomes invalid. The variables stored in HttpApplication object are for all users accessing the program. Even if a user closes the browser, the value of the variables will not be lost.
6. Cross-page Transfer
Cross-page Transfer is similar to the Transfer method that calls the HttpServerUtility object, but it is more efficient. Because the Transfer method that calls the HttpServerUtility object is a server-based method, and cross-page Transfer is based on the browser. This method is mainly used to set the "PostBackUrl" attribute of the control, so that the control (such as Button) is switched to the specified page after the operation, in addition, this specified page can directly obtain all the control objects and their attribute values on the previous page. Assume that the first page is SendValues. aspx, and add two controls in the page (not *. cs code file), as shown in the following code.
<Asp: TextBox runat = "server" ID = "PbValue" Text = "Geforce TX280"> </asp: TextBox>
<Asp: Button runat = "server" ID = "btn6" Text = "cross-page transfer method" PostBackUrl = "~ /GetValues. aspx "/>
In the code above, the "Text" attribute of the TextBox Control is "Geforce TX280", which is the value to be transferred. The "PostBackUrl" attribute of the Button control specifies the GetValues. aspx page, which can receive the value to be passed. In the Page_Load method of GetValues. aspx. cs, write the following code.
// PreviousPage is the page for transferring controls to the current page
If (PreviousPage! = Null)
{
// Search for the control whose "ID" is PbValue from the PreviousPage container and convert it to the TextBox type
TextBox tb = (TextBox) PreviousPage. FindControl ("PbValue ");
// If tb is not a null reference
If (tb! = Null)
{
// Assign the tb "Text" attribute value to the str variable
String str = tb. Text;
}
}
The above code can easily obtain the "Text" attribute value of the TextBox Control in the previous page. This method is faster than the Transfer method that calls the HttpServerUtility object and reduces the processing steps.
The above is a commonly used cross-page value transfer method. If you have special requirements, you can also use other methods, such as storing temporary data through the database.

 

From: http://www.it560.com/program/.net/0MMDAwMDAwMzk0MQ.html

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.