ASP Network Programming: A summary of data transfer methods between pages in Web programs

Source: Internet
Author: User
Tags httpcontext

We always encounter situations where we need to pass values from one Web page to another. In this article, I show you several ways to pass numeric values from one Web page to another. In this example, the page created consists of a text control and several button controls. The data entered in the text box is passed from one Web page to another through different methods that are identified in the button control.

Response.Redirect

Let's take a look at how to pass data using the Response.Redirect method first. This is the easiest way to do it. Enter some data in the text box, and when you have entered the completed data, press the "Respose.redirect" button. We get a hint that sometimes we want to pass another page in the catch program, which means catching the exception and passing it to another page. If you try to do this, it will give you a System.Threading exception program. Because you want to leave the next thread to pass data to another page, the exception is thrown.

Response.Redirect ("Webform5.aspx", false);

This statement tells the compiler to navigate to "webform5.aspx", where "false" means that the current page does not end what you are doing. You should take a look at the System.Threading class for the thread release command. Below, take a look at the C # Code of the button event. The name of the "txtname" text control, and the value inside the text box is passed to a webpage called "webform5.aspx". In the? The "Name" symbol after is just a temporary response variable that holds the value of the text.

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

{

Value sent using HttpResponse

Response.Redirect ("Webform5.aspx? Name= "+txtname.text);

}

Well, to this point, you use response to send a value. Just now, I've collected these values here, so in the "Webform5.aspx" Page_Load event, write the code. First, we check that the value entered is not NULL. If that's not the case, we're simply using the label control to display the value on the page. Note: If you use the Response.Redirect method to pass these values, all of these values are not visible in the URL of the browser. You must not use Response.Redirect to pass credit numbers and confidential information.

if (request.querystring["Name"]!= null)

Label3.text = request.querystring["Name"];

Cookies

Next use cookies. Cookies are created on the server side, but the client is omitted. In the Click event of this "Cookies" button, write the following code:

HttpCookie cName = new HttpCookie ("Name");

Cname.value = txtName.Text;

RESPONSE.COOKIES.ADD (CName);

Response.Redirect ("webform5.aspx");

First, create a cookie named "CName". Since a cookie instance can have many values, tell the compiler that the cookie holds the "Name" value. We assign it to a TextBox and add it to the response stream after the knot, and then use the Response.Redirect method to pass it to other pages.

Let's take a look at how to get the value of a cookie being passed on another page.

if (request.cookies["Name"]! = NULL)

Label3.text = request.cookies["Name"]. Value;

As you can see, we used the same method as we did before, and we just used the request.cookies within the request.querystring. Note that some browsers do not accept cookies.

Session Variables

Next we look at the session variables, which are handled by the server. The first one is passed from the client to the server, sessions is created, and the session ends when the user closes the browser window or some unusual action occurs. Here are some ways you can use the session variable to pass a value. Below you see the session created for the user and "Name" is the keyword, as well as the famous session keyword, the keyword is assigned to the TextBox value.

Session Created

session["Name"] = txtName.Text;

Response.Redirect ("webform5.aspx");

The code below shows how to get the session value.

This code must is placed in the other page.

if (session["Name"]! = NULL)

Label3.text = session["Name"]. ToString ();

Application Variables

Sometimes, we need to access values from anywhere on the page. Because of that, you can use the application variable. Here's a small piece of code that shows how to do it. Once the application variable is created and assigned a value, it can be re-obtained anywhere in the application.

This sets the value of the application Variable

application["Name"] = txtName.Text;

Response.Redirect ("webform5.aspx");

This is how we retrieve the value of the application Variable

if (application["Name"]! = NULL)

Label3.text = application["Name"]. ToString ();

HttpContext

You can use HttpContext to get the values back from the Web page. These values are obtained by using the properties of the method. Since they are easy to write code and modify, using attributes is a good way. In your first page, create a property that returns the value of the TextBox.

public string GetName

{

get {return txtname.text;}

}

We use Server.Transfer to send this control to a new page. Note: Server.Transfer only passes this control to the new page and does not reposition the page, which means you will see the address of the old page in the URL. Simply click the event in the "Server.Transfer" button, and add the following code.

Server.Transfer ("webform5.aspx");

Now, let's locate the page, and the value is passed to the page, in which case the page used is "webform5.aspx".

Can declare this globally or in any event

WEBFORM4 W;

Gets the page.context which is associated with this Page

W = (WEBFORM4) Context.Handler;

Assign the Label control with the property "GetName" which returns string

Label3.text = W.getname;

Special Note

Pay special attention to what you see, there are different ways to pass values from one page to another. Each method has its own merits and disadvantages. So, when you pass a value, choose what you need so you have a good way to do it, which is most feasible for you. If you do not understand can contact Shanghai treatment Impotence Hospital program in detail oh?

ASP Network Programming: A summary of data transfer methods between pages in Web programs

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.