Advantages and disadvantages of passing values between ASP. NET pages

Source: Internet
Author: User

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. You can use hyperlink text to transfer values on the *. ASPX page, as shown in the following figure. Code .
<Asp: hyperlink runat = "server" id = "maid"
Navigateurl = "~ /Getvalues. aspx? Urlvalue1 = intel & urlvalue2 = amd "> URL-based value transfer </ASP: hyperlink>
The above link will jump to the getvalues. ASPX page, and pass the urlvalue1 variable and urlvalue2 variable through URL, whose 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 ();
The session can be applied in Program Multiple pages are shared by name/value pairs until the browsing user closes his browser or the server session times out (you can set it to 20 minutes by default ).
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 login information of a user login will be written into the cookie file of the user's computer. The website will automatically read the cookie for authentication 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 following code adds a cookie.
// 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, that is, 6 minutes later the expiration time expires.
CK. expires = datetime. Now. addseconds ;
// 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 up to 4096 bytes of cookies. 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 browsers also limit the total number of cookies from all sites, which 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 on another page by exposing object attributes. 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;
It can be seen that 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 process data written by multiple users in the application variable. 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 a browser-based method. 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 above Code, 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.
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.