How to pass values between pages?
There are several methods to pass values from one page to another page. Described below
are few methods to pass values between pages:
QueryString - The QueryString method of passing values between web pages is one of the
oldest methods of passing values between pages. A variable value is properly encoded
before it is placed on a querystring. This is to make sure that characters that cause
problems (like symbols and spaces) are encoded correctly. See the code below to see how
QueryString functionality works.
//Code in InitialPage.aspx
String sString;
sString = Server.UrlEncode("string in InitialPage.aspx");
Response.Redirect("DestinationPage.aspx?Value=" & sString);
//Code in DestinationPage.aspx reads the QueryString
String sString;
sString = Request.QueryString("Value");
Response.Write("Your name is " & sString);
The data in the DestinationPage.aspx in the URL looks like this...
http://www.dotnetuncle.com/DestinationPage.aspx?Value=dotnetUncle
Context - The context object is used to send values between pages. Its similar to the
session object, the difference being that, the Context object goes out of scope when the page
is sent to a browser. Example code below shows how to use Context object.
'InitialPage.aspx stores value in context before sending it
Context.Items("MyData") = "dotnetuncle";
Server.Transfer("DestinationPage.aspx");
'DestinationPage.aspx retrieves the value from InitialPage.aspx's context
String sString;
sString = Context.Items("MyDate").ToString;
Response.Write("The data is as follows: " & sString);
Session - The session object is used to persist data across a user
session during the user's visit to a website. It is almost same as the Context object.
When we use Response.Redirect, it causes the Context object to go away, so rather the
Session object is used in such a scenario. Session object uses more of server memory than a
context object. Example code below shows how to use Session object.
'InitialPage.aspx stores value in session before sending it
Session.Items("MyData") = "dotnetuncle";
Response.Redirect("DestinationPage.aspx");
'DestinationPage.aspx retrieves the value from InitialPage.aspx's session
String sString;
sString = Session.Items("MyDate").ToString;
Response.Write("The data is as follows: " & sString);
You may notice above, I have used Response.Redirect with session object, and server.transfer
with a context object.
Application, Cache, Session - objects are used to store global variables. To know
more about them, Click Here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22