ArticleDirectory
-
- 1. response. Redirect, server. Transfer, server. Execute
-
- 2. Cookie usage
-
- There are two methods to clear a cookie: one is to clear the cookie on the server, and the other is to clear the cookie on the client.
-
- 3. Use querystring
-
- 4. Use Form
-
- 5. Use session Variables
-
- 6. Use server. Transfer
-
- 7. Use Application
-
- 1. response. Redirect, server. Transfer, server. Execute
-
- 2. Cookie usage
-
- There are two methods to clear a cookie: one is to clear the cookie on the server, and the other is to clear the cookie on the client.
- 3. Use querystring
-
- 4. Use Form
-
- 5. Use session Variables
-
- 6. Use server. Transfer
-
- 7. Use Application
. Net:
1. response. Redirect, server. Transfer, server. Execute
Server. Transfer:
Public string name
{
Get {
Return "Li Jinchang ";
}
Set
{
Name = value;
}
}
Reponse. Redirect () (client) 302 + URL à server à return client à client to execute the request, the passed parameter size cannot exceed 2 kb
Cute will return to the calledProgramPage.
2. Cookie usage
Protected void page_load (Object sender, eventargs E)
{
// Obtain the client cookie value when you open the logon page and write it to the foreground control.
Httpcookie cookie = request. Cookies ["name"];
If (cookie = NULL)
{
Username. Text = "";
}
Else
{
Username. Text = cookie. value;
}
}
Protected void btnsubmit_click (Object sender, eventargs E)
{
String username = request. querystring ["username"];
String Password = request. querystring ["password"];
If (logon successful)
{
Response. Write ("Login successful ");
Httpcookie cookie = new httpcookie ("name", username); // obtain the user name
Cookie. expires = datetime. Now. adddays (10); // set the cookie expiration time to 10 days later.
Response. Cookies. Add (cookie); // write the cookie to the client
}
Else
{
Response. Write ("Logon Failed ");
}
}
There are two methods to clear a cookie: one is to clear the cookie on the server, and the other is to clear the cookie on the client.
The server clears the cookie:
1. Request. Cookies. Remove (name); // clear the specified cookie
2. Request. Cookies. Clear (); // clear all cookies
Client JS operation cookie
// Read the cookies Function
Function getcookie (name)
{
VaR arr = Document. Cookie. Match (New Regexp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (Arr! = NULL) return Unescape (ARR [2]); return NULL;
}
// Delete the cookie
Function delcookie (name)
{
VaR exp = new date ();
Exp. settime (exp. gettime ()-1 );
VaR cval = newgetcookie (name );
If (cval! = NULL) document. Cookie = Name + "=" + cval + "; expires =" + exp. togmtstring ();
}
// Add cookie
Function setcookies (name, value)
{
VaR days = 30;
VaR exp = new date ();
Exp. settime (exp. gettime () + days x 24x60*60*1000 );
Document. Cookie = Name + "=" + escape (value) + "; expires =" + exp. togmtstring ();
}
3. Use querystring
Querystring is a simple value transfer method. Its disadvantage is that it displays the value to be transferred in the address bar of the browser and cannot pass objects in this method. If you want to pass a security that is not so important or a simple value, it is best to use this method. The following is a small example to complete the data transfer process. The procedure is as follows:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
CodeAs follows:
Private void button#click
(Object sender, system. eventargs E)
{
String URL;
Url = "webform2.aspx? Name = "+
Textbox1.text + "& Email =" +
Textbox2.text;
Response. Redirect (URL );
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}
Run the command to view the passed result.
4. Use Form
1. Create a web form
2. Create a form in the new web form, place a submit button in the form, and place two textbox1 and textbox2
3. Add an action to the form (Form Processing page url webform2.aspx)
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
Label1.text = request. Form ["name"];
Label2.text = request. Form ["email"];
}
5. Use session Variables
Using session variables to pass values is the most common method. In this method, values can be transmitted not only to the next page, but also to multiple pages, the variable does not disappear until the value of the session variable is removed. For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click (Object sender, system. eventargs E)
{
Session ["name"] = textbox1.text;
Session ["email"] = textbox2.text;
Response. Redirect ("webform2.aspx ");
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
Label1.text = session ["name"]. tostring ();
Label2.text = session ["email"]. tostring ();
Session. Remove ("name ");
Session. Remove ("email ");
}
Run the command to view the passed result.
6. Use server. Transfer
Although this method is a bit complicated, it is also a way to pass values on the page.
For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click (Object sender, system. eventargs E)
{
Server. Transfer ("webform2.aspx ");
}
4. The textbox1 is returned during the creation process. The value code of the textbox2 control is as follows:
Public string name
{
Get
{
Return textbox1.text;
}
}
Public String email
{
Get
{
Return textbox2.text;
}
}
5. Create a new target page named webform2
6. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
// Create an instance of the original form
Webform1 WF1;
// Obtain the instantiated handle
WF1 = (webform1) Context. Handler;
Label1.text = wf1.name;
Label2.text = wf1.email;
}
Run the command to view the passed result.
Another method:
Sending page: Server. Transfer ("target. aspx? Param1 = 1111? M2 = 2222 ")
Receiving page: String STR = request ["param1"]
7. Use Application
Sending page: Application ("param1") = "1111 ";
By page: String STR = Application ("param1"). tostring ();
This trick is not often used. Because the application is shared across the only application domain, all users can change and set its value. Therefore, they only need to use counters and other global variables.
Application can be viewed as the global attribute of the server, and session can be viewed as the global attribute of the site. You can delete the application by removing session. Clear ();
Session. removeall ();
. Net:
1. response. Redirect, server. Transfer, server. Execute
Server. Transfer:
Public string name
{
Get {
Return "Li Jinchang ";
}
Set
{
Name = value;
}
}
Reponse. Redirect () (client) 302 + URL à server à return client à client to execute the request, the passed parameter size cannot exceed 2 kb
Cute will return to the called program page after execution.
2. Cookie usage
Protected void page_load (Object sender, eventargs E)
{
// Obtain the client cookie value when you open the logon page and write it to the foreground control.
Httpcookie cookie = request. Cookies ["name"];
If (cookie = NULL)
{
Username. Text = "";
}
Else
{
Username. Text = cookie. value;
}
}
Protected void btnsubmit_click (Object sender, eventargs E)
{
String username = request. querystring ["username"];
String Password = request. querystring ["password"];
If (logon successful)
{
Response. Write ("Login successful ");
Httpcookie cookie = new httpcookie ("name", username); // obtain the user name
Cookie. expires = datetime. Now. adddays (10); // set the cookie expiration time to 10 days later.
Response. Cookies. Add (cookie); // write the cookie to the client
}
Else
{
Response. Write ("Logon Failed ");
}
}
There are two methods to clear a cookie: one is to clear the cookie on the server, and the other is to clear the cookie on the client.
The server clears the cookie:
1. Request. Cookies. Remove (name); // clear the specified cookie
2. Request. Cookies. Clear (); // clear all cookies
Client JS operation cookie
// Read the cookies Function
Function getcookie (name)
{
VaR arr = Document. Cookie. Match (New Regexp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (Arr! = NULL) return Unescape (ARR [2]); return NULL;
}
// Delete the cookie
Function delcookie (name)
{
VaR exp = new date ();
Exp. settime (exp. gettime ()-1 );
VaR cval = newgetcookie (name );
If (cval! = NULL) document. Cookie = Name + "=" + cval + "; expires =" + exp. togmtstring ();
}
// Add cookie
Function setcookies (name, value)
{
VaR days = 30;
VaR exp = new date ();
Exp. settime (exp. gettime () + days x 24x60*60*1000 );
Document. Cookie = Name + "=" + escape (value) + "; expires =" + exp. togmtstring ();
}
3. Use querystring
Querystring is a simple value transfer method. Its disadvantage is that it displays the value to be transferred in the address bar of the browser and cannot pass objects in this method. If you want to pass a security that is not so important or a simple value, it is best to use this method. The following is a small example to complete the data transfer process. The procedure is as follows:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, system. eventargs E)
{
String URL;
Url = "webform2.aspx? Name = "+
Textbox1.text + "& Email =" +
Textbox2.text;
Response. Redirect (URL );
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}
Run the command to view the passed result.
4. Use Form
1. Create a web form
2. Create a form in the new web form, place a submit button in the form, and place two textbox1 and textbox2
3. Add an action to the form (Form Processing page url webform2.aspx)
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
Label1.text = request. Form ["name"];
Label2.text = request. Form ["email"];
}
5. Use session Variables
Using session variables to pass values is the most common method. In this method, values can be transmitted not only to the next page, but also to multiple pages, the variable does not disappear until the value of the session variable is removed. For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click (Object sender, system. eventargs E)
{
Session ["name"] = textbox1.text;
Session ["email"] = textbox2.text;
Response. Redirect ("webform2.aspx ");
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
Label1.text = session ["name"]. tostring ();
Label2.text = session ["email"]. tostring ();
Session. Remove ("name ");
Session. Remove ("email ");
}
Run the command to view the passed result.
6. Use server. Transfer
Although this method is a bit complicated, it is also a way to pass values on the page.
For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click (Object sender, system. eventargs E)
{
Server. Transfer ("webform2.aspx ");
}
4. The textbox1 is returned during the creation process. The value code of the textbox2 control is as follows:
Public string name
{
Get
{
Return textbox1.text;
}
}
Public String email
{
Get
{
Return textbox2.text;
}
}
5. Create a new target page named webform2
6. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
// Create an instance of the original form
Webform1 WF1;
// Obtain the instantiated handle
WF1 = (webform1) Context. Handler;
Label1.text = wf1.name;
Label2.text = wf1.email;
}
Run the command to view the passed result.
Another method:
Sending page: Server. Transfer ("target. aspx? Param1 = 1111? M2 = 2222 ")
Receiving page: String STR = request ["param1"]
7. Use Application
Sending page: Application ("param1") = "1111 ";
By page: String STR = Application ("param1"). tostring ();
This trick is not often used. Because the application is shared across the only application domain, all users can change and set its value. Therefore, they only need to use counters and other global variables.
Application can be viewed as the global attribute of the server, and session can be viewed as the global attribute of the site. You can delete the application by removing session. Clear ();
Session. removeall ();