Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET
Article

Transferring page values to another page

Rate me:
Please Sign up or sign in to vote.
4.75/5 (57 votes)
21 Aug 20044 min read 534.8K   85   32
Transferring page values to another page.

Sample screenshot

Introduction

We always come into situations in which we need to transfer values from one page to another page. In this article, I will show you some ways of transferring values from page to page. The page I created in this example is really simple which consists of a text field and a few buttons. The data entered in the text field will be transferred to another page by using different methods labeled on the buttons.

Using the code

Response.Redirect

Let's first see how to transfer using Response.Redirect method. This maybe the easiest of them all. You start by writing some data in the text field, and when you finish writing the data, you press the button labeled 'Reponse.Redirect'. One tip that I would like to share with you is, sometimes we want to transfer to another page inside the catch exception, meaning exception is caught and we want to transfer to another page. If you try to do this, it may give you a System.Threading exception. This exception is raised because you are transferring to another page leaving behind the thread running. You can solve this problem using:

C#
Response.Redirect("WebForm5.aspx",false);

This tells the compiler to go to page "WebForm5.aspx", and "false" here means that don't end what you were doing on the current page. You should also look at the System.Threading class for threading issues. Below, you can see the C# code of the button event. "txtName" is the name of the text field whose value is being transferred to a page called "WebForm5.aspx". "Name" which is just after "?" sign is just a temporary response variable which will hold the value of the text box.

C#
private void Button1_Click(object sender, System.EventArgs e)
{
    // Value sent using HttpResponse
    Response.Redirect("WebForm5.aspx?Name="+txtName.Text);
}

Okay, up till this point, you have send the values using Response. But now, where do I collect the values, so in the "WebForm5.aspx" page_Load event, write this code. First, we check that the value entered is not null. If it's not, then we simply display the value on the page using a Label control. Note: When you use Response.Redirect method to pass the values, all the values are visible in the URL of the browser. You should never pass credit card numbers and confidential information via Response.Redirect.

C#
if (Request.QueryString["Name"]!= null)
    Label3.Text = Request.QueryString["Name"];

Cookies

Next up is cookies. Cookies are created on the server side but saved on the client side. In the button click event of 'Cookies', write this code:

C#
HttpCookie cName = new HttpCookie("Name");
cName.Value = txtName.Text; 
Response.Cookies.Add(cName); 
Response.Redirect("WebForm5.aspx");

First, we create a cookie named "cName". Since one cookie instance can hold many values, we tell the compiler that this cookie will hold "Name" value. We assign to it the value of the TextBox and finally add it in the Response stream, and sent it to the other page using Response.Redirect method.

Let's see here how we can get the value of the cookie which is sent by one page.

C#
if (Request.Cookies["Name"] != null )
    Label3.Text = Request.Cookies["Name"].Value;

As you see, it's exactly the same way as we did before, but now we are using Request.Cookies instead of Request.QueryString. Remember that some browsers don't accept cookies.

Session Variables

Next we see the session variables which are handled by the server. Sessions are created as soon as the first response is being sent from the client to the server, and session ends when the user closes his browser window or some abnormal operation takes place. Here is how you can use session variables for transferring values. Below you can see a Session is created for the user and "Name" is the key, also known as the Session key, which is assigned the TextBox value.

C#
// Session Created
Session["Name"] = txtName.Text; 
Response.Redirect("WebForm5.aspx");

// The code below shows how to get the session value.
// This code must be placed in other page.
if(Session["Name"] != null) 
    Label3.Text = Session["Name"].ToString();

Application Variables

Sometimes, we need to access a value from anywhere in our page. For that, you can use Application variables. Here is a small code that shows how to do that. Once you created and assigned the Application variable, you can retrieve its value anywhere in your application.

C#
// 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 also use HttpContext to retrieve values from pages. The values are retrieved using properties or methods. It's a good idea to use properties since they are easier to code and modify. In your first page, make a property that returns the value of the TextBox.

C#
public string GetName
{ 
    get { return txtName.Text; }
}

We will use Server.Transfer to send the control to a new page. Note that Server.Transfer only transfers the control to the new page and does not redirect the browser to it, which means you will see the address of the old page in your URL. Simply add the following line of code in 'Server.Transfer' button click event:

C#
Server.Transfer("WebForm5.aspx");

Now, let's go to the page where the values are being transferred, which in this case is "webForm5.aspx".

C#
// You can declare this Globally or in any event you 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

As you see, there are various method in transferring values from one page to another. Each method has its own advantage and disadvantage. So, when you are transferring values, do your homework so that you have a better idea which approach is most feasible for you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

 
Questiongetting 'Unable to cast object of type ' error for the above httpcontext method Pin
Member 130142332-Jul-17 20:58
Member 130142332-Jul-17 20:58 
QuestionCompare them Pin
Simpa Godwin23-Mar-17 5:04
Simpa Godwin23-Mar-17 5:04 
QuestionPlease suggest Pin
Member 1178074020-Jun-15 20:58
Member 1178074020-Jun-15 20:58 
GeneralMy vote of 5 Pin
Purushotham Agaraharam16-Mar-15 3:57
Purushotham Agaraharam16-Mar-15 3:57 
Questionvery nice Pin
arun sahani19-Mar-14 22:02
arun sahani19-Mar-14 22:02 
GeneralMy vote of 5 Pin
Debopam Pal25-Nov-13 0:56
professionalDebopam Pal25-Nov-13 0:56 
Generalexcellent Pin
mosi9814-Jul-13 21:55
mosi9814-Jul-13 21:55 
GeneralVery good Article Pin
Fahim saqib18-Nov-12 19:40
Fahim saqib18-Nov-12 19:40 
GeneralMy vote of 5 Pin
sweet_zs12-Oct-12 16:27
sweet_zs12-Oct-12 16:27 
QuestionURL REDIRECTION Pin
Member 917112823-Sep-12 20:15
Member 917112823-Sep-12 20:15 
QuestionSending many values in session Pin
Member 805738224-Aug-12 18:57
Member 805738224-Aug-12 18:57 
QuestionHow to get a value from a table and pass to next page? Pin
Member 805738223-Aug-12 18:40
Member 805738223-Aug-12 18:40 
QuestionCan i have the same code for Response.Redirect in vb.net please? Pin
ramamadhavi3-Aug-12 4:14
ramamadhavi3-Aug-12 4:14 
GeneralMy vote of 5 Pin
Pankil_Plus18-May-12 16:13
Pankil_Plus18-May-12 16:13 
Questionhow to detect button click Pin
helloarun7922-Nov-11 8:33
helloarun7922-Nov-11 8:33 
GeneralMy vote of 5 Pin
Adityakrishna8-Nov-11 15:27
Adityakrishna8-Nov-11 15:27 
GeneralMy vote of 5 Pin
anurag129012-Oct-11 4:15
anurag129012-Oct-11 4:15 
GeneralMy vote of 5 Pin
shwetabothra25-Oct-10 21:46
shwetabothra25-Oct-10 21:46 
QuestionHow to transfer secure data from aspx page to paypal...which transfer mathed is we have to use....? [modified] Pin
jigarchaudhari10-Aug-08 0:39
jigarchaudhari10-Aug-08 0:39 
GeneralNeeded directive Pin
FredParcells26-Feb-07 9:26
FredParcells26-Feb-07 9:26 
GeneralCheckboxes... Pin
fuhaizah18-Sep-05 17:45
fuhaizah18-Sep-05 17:45 
GeneralRe: Checkboxes... Pin
enjoycrack18-Sep-05 20:09
enjoycrack18-Sep-05 20:09 
QuestionWhat if it's complex data Pin
shiliang21-Jun-05 17:28
shiliang21-Jun-05 17:28 
What if I need transfer a complex data structure, say, a list of records, can you please tell me how to do that.
AnswerRe: What if it's complex data Pin
enjoycrack18-Sep-05 20:11
enjoycrack18-Sep-05 20:11 
GeneralTransfer 2 values Pin
izazz24-Feb-05 15:15
izazz24-Feb-05 15:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.