Five ways to transfer values between pages [HTML]

Source: Internet
Author: User

First, QueryString value:
1. This is the simplest way to pass the value, but the disadvantage is that the value of the pass will be displayed in the browser's address bar and cannot pass the object, only for passing simple and security requirements are not high integer values, for example:

2. Create a new Web project, add a page named Test1, add a button to the page named Btnlogin, add two textbox named txtUserName and Txtpassword, and Add the button click () Event:

private void Btnlogin_click (object sender, System.EventArgs e)

{

String Url= "Test1.aspx? Username= "+txtusername.text +" &password= "+txtpassword.text+" ";

Response.Redirect (URL);

}

3. Add another page named Test2, add two lable on the page named Lblusername and Lblpassword, add the page load () event:

private void Page_Load (object sender, System.EventArgs e)

{

lblusername.text=request.querystring["UserName"];

lblpassword.text=request.querystring["Password"];

}

4. Set the Test1 as the start page, run the project in the Test1 page text box to enter the value after the Click button, you can display Test1 page input results in the Test2 page.

Second, Server.Transfer value:
1. This method avoids the value to be passed in the browser's address bar, but it is troublesome, for example:

2. Create a new Web project, add two pages named Test1 and Test2, add a button to the Test1 page named Btnlogin, and add two textbox named txtUserName and Txtpassword, respectively. On the Test2 page, add two lable named Lblusername and Lblpassword respectively, return the Test1 and txtUserName values for the Txtpassword add process, and add the Btnlogin click () event:

public string UserName

{

Get

{

return txtusername.text;

}

}

public string Password

{

Get

{

return txtpassword.text;

}

}

private void Btnlogin_click (object sender, System.EventArgs e)

{

Server.Transfer ("test2.aspx");

}

3. Add the Load () event for the Test2 page:

private void Page_Load (object sender, System.EventArgs e)

{

Test1 T1; Create an instance of the original form

t1= (Test1) Context.Handler; Get the instantiated handle

lblusername.text= t1. UserName;

lblpassword.text= t1. Password;

}

4. Set the Test1 as the start page, run the project in the Test1 page text box to enter the value after the Click button, you can display Test1 page input results in the Test2 page.

Third, the cookie object variable:
1. Cookies are for each user and are stored on the client, and cookies are used in conjunction with the ASP. NET built-in object request, for example:

2. Create a new Web project, add two pages named Test1 and Test2, add a button to the Test1 page named Btnlogin, and add two textbox named txtUserName and Txtpassword, respectively. On the Test2 page, add two lable, named Lblusername and Lblpassword, to add the button's click () event for Test1:

private void Btnlogin_click (object sender, System.EventArgs e)

{

HttpCookie cookie_username = new HttpCookie ("UserName");

HttpCookie Cookie_password = new HttpCookie ("PassWord");

Cookie_ username.value = txtUsername.Text;

Cookie_ password.value = txtPassword.Text;

Response.appendcookie (Cookie_ UserName);

Response.appendcookie (Cookie_ PassWord);

Server.Transfer ("test2.aspx");

}

3. Add the Load () event for the Test2 page:

private void Page_Load (object sender, System.EventArgs e)

{

Lblusername.text = request.cookies["UserName"]. Value.tostring ();

Lblpassword.text = request.cookies["PassWord"]. Value.tostring ();

}

4. Set the Test1 as the start page, run the project in the Test1 page text box to enter the value after the Click button, you can display Test1 page input results in the Test2 page.

Iv. Session Object variables:
1. The session is also for each user, is stored on the server side, the session can not only pass the value to the next page, but also cross-pass to multiple pages, until the value of the session variable removed, the variable will disappear, for example:

2. Create a new Web project, add two pages named Test1 and Test2, add a button to the Test1 page named Btnlogin, and add two textbox named txtUserName and Txtpassword, respectively. On the Test2 page, add two lable, named Lblusername and Lblpassword, to add the button's click () event for Test1:

private void Btnlogin_click (object sender, System.EventArgs e)

{

session["UserName"]=txtusername.text;

session["PassWord"]=txtpassword.text;

Response.Redirect ("test2.aspx");

}

3. Add the Load () event for the Test2 page:

private void Page_Load (object sender, System.EventArgs e)

{

lblusername.text=session["UserName"]. ToString ();

lblpassword.text=session["Password"]. ToString ();

Session.remove ("UserName"); Clear session

Session.remove ("PassWord"); Clear session

}

4. Set the Test1 as the start page, run the project in the Test1 page text box to enter the value after the Click button, you can display Test1 page input results in the Test2 page.

V. Application Object variables:
1. The Application object is scoped to the whole global, which means it is valid for all users. Its common methods are with lock and unlock, for example:

2. Create a new Web project, add two pages named Test1 and Test2, add a button to the Test1 page named Btnlogin, and add two textbox named txtUserName and Txtpassword, respectively. On the Test2 page, add two lable, named Lblusername and Lblpassword, to add the button's click () event for Test1:

private void Btnlogin_click (object sender, System.EventArgs e)

{

application["UserName"] = txtUsername.Text;

application["PassWord"] = txtPassword.Text;

Server.Transfer ("test2.aspx");

}

3. Add the Load () event for the Test2 page:

private void Page_Load (object sender, System.EventArgs e)

{

Application.Lock ();

Lblusername. Text = application["UserName"]. ToString ();

Lblpassword. Text = application["PassWord"]. ToString ();

Application.UnLock ();

}

4. Set the Test1 as the start page, run the project in the Test1 page text box to enter the value after the Click button, you can display Test1 page input results in the Test2 page.

Five ways to transfer values between pages [HTML]

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.