A summary of data transfer methods between Web pages in Web programs

Source: Internet
Author: User
Tags httpcontext numeric value thread tostring
web| Program | data | Web page Introduction

We always encounter situations where we need to pass values from one Web page to another. In this article, you are presented with several ways to transfer values from one Web page to another. In this example, the Web 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 first look at how to use the Response.Redirect method to pass data. This is the easiest way to do it. Enter some data in the text box, and when you enter the completion data, press the "Respose.redirect" button. We get a hint that sometimes we want to pass another page in a catch program, which means catching an 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 Publishing command. Below, take a look at the C # code for the button event. The name of the Txtname text control, and the value within the text box is passed to a Web page called "webform5.aspx." In "? The "Name" symbol is 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, by this point, you use response to send a number. Just now, I've collected these values here, so write that code in the "Webform5.aspx" Page_Load event. First, we check that the value entered is not NULL. If not, we simply use the label control on the Web page to display the values. 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 the letter of credit number 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 are omitted by the client. In the Click event of this "cookie" 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 a number of values, tell the compiler that the cookie holds the "Name" value. We assign it to the textbox and then add it to the response stream after the knot, and then use the Response.Redirect method to pass it to the other page.

Let's take a look at how to get the cookie values passed by another page.

if (request.cookies["Name"]!= null)
Label3.text = request.cookies["Name". Value;
As you can see, just like we used to do one of the same methods, we have only in the request.querystring, the use of request.cookies. Note that some browsers do not receive cookies.

Session Variables

Next we take a look at the session variables, which are handled by the server. First Shadow 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 actions occur. Here are some ways you can use the session variable to pass a numeric value. Below you can see that the session and "Name" created for the user are keywords, as well as the well-known session keyword, where keywords are assigned to textbox values.

Session Created

session["Name"] = txtName.Text;
Response.Redirect ("webform5.aspx");

The code below shows 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 web. Because of that, you can use the application variable. Here's a little piece of code that shows how to do those things. Once you create a application variable and assign it a value, you can regain its value 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. Get those values by using the properties of the method. Since they are easy to code and modify, using attributes is a good way to do it. In your first page, you 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 simply 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 is "webform5.aspx".

Can declare this globally, or in any event, like

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 transfer values from one Web page to another. Each method has its own advantages and disadvantages. So, when you pass a value, choose what you need, so you have a good way of doing it, which is most feasible for you.



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.