This paper examines whether the interviewer's understanding of multiple pages in ASP.net is comprehensive. Because the ASP.net page form is submitted to itself and completes the return function, it is not possible by default to use post for multiple page transfer values. The analysis of these transfer modes is shown below.
1. URL Pass Value This is a classic way of passing values, and this method is very simple to use, but the values passed are displayed on the browser's address bar and cannot be passed. So this method is generally used in cases where the value is low and the security requirements are not high. You can use hyperlink text to pass values in *.aspx page development, as shown in the following code.
<asp:hyperlink runat= "Server" id= "Hplink"
Navigateurl= "~/GETVALUES.ASPX?URLVALUE1=INTEL&URLVALUE2=AMD" >url transfer value </asp:HyperLink>
The above link text jumps the page to the Getvalues.aspx page and passes the urlvalue1 variables and urlvalue2 variables via the URL, respectively, for Intel and AMD. You can also call the redirect method of the HttpResponse object in the *.cs server-side method, directing the browser side to the new page and passing the value through the URL, as shown in the following code.
Response.Redirect ("~/GETVALUES.ASPX?URLVALUE1=INTEL&URLVALUE2=AMD");
2. Session Pass Value This method stores each data in the server variable, can pass more data, and the security is high, so it is often used in the authentication function of the user. However, if the session variable stores too much data consumes too much server resources, programmers should be cautious when using them, as shown in the following code.
session["Sessionvalue"] = "ATI";
can also be stored with an index
Session[0] = "ATI";
The server-side methods in other pages are evaluated in the following ways:
String str = session["Sessionvalue"]. ToString ();
You can also use the index to get
String str = session[0]. ToString ();
Sessions can be shared by name/value pairs on multiple pages of an application until the browsing user closes their browser or the server session timeout (can be set by default of 20 minutes).
3. Cookie Pass Value Cookies are a special way of storing data, because it stores the data in the user's computer as a text file in the form of a disk. This way is very interesting, many login system is to use cookies to achieve user automatic login. That is, the logon information that a user logs in to will be written to a cookie file in the user's computer, and the Web site automatically reads the cookie at the next logon to complete the authentication. Although it is convenient to pass data through cookies, save time can be set freely, but security is not high, programmers should not rely too much on cookies, but should use a combination of the way to complete the storage of sensitive data.
Asp. The main types of operation cookies in net are in the System.Web namespace, HttpCookie, HttpResponse, and HttpRequest respectively. HttpCookie can establish and manipulate the security type of a standalone cookie, and access the HttpResponse and HttpRequest cookies properties to get HttpCookieCollection collection objects. To read or add a HttpCookie object. The method for adding cookies is shown in the following code.
Create a HttpCookie object and pass the name value to the constructor
HttpCookie ck = new HttpCookie ("Cookievalue");
Set the "Value" property of CK (value)
Ck. Value = "Cookie values";
Set the expiration time of CK to the current time plus, i.e. 6 minutes after expiration
Ck. Expires = DateTime.Now.AddSeconds;
Set CK valid range (domain limit)
Ck. Domain = "www.google.cn";
Add CK to the HttpCookieCollection object
RESPONSE.COOKIES.ADD (CK);
You can also add the following methods for the
Response.appendcookie (CK);;
It is simpler to read a user's cookie by simply reading a subkey from the HttpCookieCollection collection object by name, as shown in the following code.
String str = request.cookies["Cookievalue"]. Value;
In addition, you can use a 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 as a name/value pair
Ck. Values.add ("Value1", "onE ");
Ck. Values.add ("value2", "two");
Set the expiration time of CK to the current time plus 100 years, or 100 years later
Ck. Expires = DateTime.Now.AddYears (100);
Add in the following two ways
RESPONSE.COOKIES.ADD (CK);
Response.appendcookie (CK);
Read the cookies as shown in the following code.
String str = "value 1:" +request.cookies["Cookievalue"]. values["Value1"];
str = = "Value 2:" + request.cookies["Cookievalue"]. values["value2"];
Note: Most browsers support cookies up to 4096 bytes, and if you want to save a few values to a user's computer, the browser also restricts the number of cookies each site can save on a user's computer. Most browsers allow only 20 cookies to be saved per site. If you try to save more cookies, the first saved cookies are deleted. There are also browsers that limit the total number of cookies from all sites, which is typically 300. Users can set their own browsers and refuse to accept cookies, which can only be combined with other ways of storing data.
4. Server.Transfer Pass Value There are relatively many steps in this method, and it is object-oriented to use the method programmer to access the value on another page in a way that exposes the properties of the object. The code for this method is not complex, first by defining the property of a public permission that returns the value that you want to pass. Then, on page two, using the Context.Handler property to get a reference to the previous page instance object, you can get the value you want by accessing the custom property.
Assuming that the Sendvalues.aspx page is a page that passes data, a public property is defined in the SendValues.aspx.cs page class that returns the value that needs to be passed directly (the property value of a server control on the page).
public string TransferData
{
Get
{
return "VIA";
}
}
Call the transfer method of the HttpServerUtility object in a method of SendValues.aspx.cs, such as the Click event handling method of a button control, as shown in the following code.
Server.Transfer ("~/getvalues.aspx");
Suppose Getvalues.aspx is the second page and receives the value passed by the previous page in the GetValues.aspx.cs Page_Load method, as shown in the following code.
Define variable sv_page for sendvalues type
Sendvalues Sv_page;
Accesses the handler property of the HttpContext object, converts the value to the Sendvalues type, and references the assignment to the Sv_page
Sv_page = (sendvalues) Context.Handler;
Sv_page is the object of the Sendvalues.aspx page class and gets its TransferData property value directly
string str = Sv_page.transferdata;
As you can see, this approach 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 needs to define too many attributes, slightly complicated. Programmers can also add data for multiple name/value pairs directly using the "Iterms" attribute of the HttpContext object. In a method that SendValues.aspx.cs calls the transfer method of the HttpServerUtility object, the code is modified as shown below.
context.items["value1"] = "onE ";
context.items["value2"] = "two";
Receive the values passed by the first 1 pages in the GetValues.aspx.cs Page_Load method, and modify the code as shown in the following code.
String str = "Value 1:" + context.items["value1"]. ToString ();
str = = "Value 2:" + context.items["value2"]. ToString ();
5. Application Pass Value Strictly speaking, it is necessary to generate a state variable on the server side of the HttpApplication object to store the required information, and the available scope of the HttpApplication object variable covers the entire Web application. So the object generally stores some information to be published, such as the number of online people, and for those sensitive data that involves the user's individual, it is not stored in this way. The HttpApplication object has two commonly used methods, the lock and unlock methods, that can be used to handle the problem of writing to data stored in application variables by multiple users. The Lock method locks all application variables, preventing other users from modifying the Application object's variable values, and unlock the method to unlock the HttpApplication object variable. The method of passing the value through the HttpApplication object is similar to the session, and writing the code in a method of the page class is shown below.
Application["a"] = "Microsoft Company";
Application["B"] = "apple Company";
Application["C"] = "MAC";
The above code is easy to save three string values in the HttpApplication object's variables, and these variables are shared by the entire program, other users through the corresponding page can also be obtained. The methods for obtaining these variables are shown in the following code.
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 session, the session is for each individual user, and the session fails when the user closes the browser. The variable stored by the HttpApplication object is a user of all access programs, and the value of the variable is not lost even if a user closes the browser.
6. Cross-page Routing The transfer method of transferring and invoking HttpServerUtility objects across pages is similar, but more efficient. Because the transfer method of calling the HttpServerUtility object is a server-based approach, cross-page routing is based on the browser side. This method is primarily to set the control's "PostBackUrl" property so that the control, such as a button, moves to the specified page, and the specified page can directly get all the control objects and their property values from the previous page. Assuming that the first page is sendvalues.aspx, add two controls to the page (not *.cs code files), 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 routing" postbackurl= "~/getvalues.aspx"/>
In the above code, the TextBox control's "Text" property is "Geforce TX280", which is the value you want to transfer. The button control's "PostBackUrl" property specifies a getvalues.aspx page that can receive the values that you want to pass. In the GetValues.aspx.cs Page_Load method, write the following code.
PreviousPage is the page that transmits the control to the current page
if (previouspage!= null)
{
Search for a control with "ID" as pbvalue from the PreviousPage container and convert to a textbox type
TextBox TB = (textbox) Previouspage.findcontrol ("Pbvalue");
If TB is not a null reference
if (TB!= null)
{
The TB "Text" attribute value to the str variable
String str = tb. Text;
}
}
The code above easily obtains the "Text" property value of the TextBox control in the previous page, which is quicker than the transfer method of invoking the HttpServerUtility object and reduces the processing steps.
The above is a commonly used method of page transfer, if you have special needs, you can also use other methods, such as the storage of temporary data through the database.
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.