Saturday, April 18, 2009

Passing data/parameters/values from one aspx page to another aspx page

Please visit my new Web Site

Passing parameters from one page to another page is a very common task in Web development. Specially when you develop a project & redirects or navigates from one ASP.NET Web page to another, you will frequently want to pass information from the source page to the target page . In this post, I will show you some ways of transferring data/parameters/values between one page to another page which is also termed as State Management. The pages I created to show you the example is really simple which consists of a text field and a button in the first page named as page1 and a blank second page named as page2.

According to the characteristics of aspx page lifecycle, web form do not retain their value after a page is displayed. To resolve this problem, ASP.NET provides the following ways to retain variables between pages:

*** Query Strings
*** Cookies
*** Session variables
*** Server.Transfer
*** Post Back URL

Let we have two aspx page named page1.aspx(contains txtName TextBox, txtAge TextBox & cmdTransfer Command Button) & a blank page2.aspx

Using Query String:
The most common & easiest way of passing data is query string. Usually we pass value(s) through query string of the page and then this value is pulled from Request object in another page. One thing keep in mind that user can view query string value any time(from brwser address, View source, even on mouse over of a link etc.). So never pass information which is secure like password through query string. Query string appear after a question mark of a hyperlink & you can add more by adding & Like:
<a href=''>......</a>

Below is an example:
Suppose you want to pass the txtName TextBox value from page1 to page2 on cmdTransfer button click event:
protected void cmdTransfer_Click(object sender, EventArgs e)
Response.Redirect("page2.aspx?myname=" + txtName.Text);

Now the question is how we can read/retrieve my name from page2.aspx page? The way is:
string myname = Request.QueryString["myname"];

Now more advance is if you want to transfer special charaters or a line with spaces then you must have to use Server.UrlEncode method before transfer & Server.UrlDecode method to read data from page2.aspx:
protected void cmdTransfer_Click(object sender, EventArgs e)
Response.Redirect("page2.aspx?myname=" + Server.UrlEncode(txtName.Text));

and to read from page2:
string myname = Server.UrlDecode(Request.QueryString["myname"]);

Now i want to show you how you can transfer more data:
protected void cmdTransfer_Click(object sender, EventArgs e)

Now read both name & age from page2 in the same way:
string myname = Request.QueryString["myname"];
string myage = Request.QueryString["myage"];

Now i want to show you how you can detect does a parameter contains value or not.
So we have to modify read data section in page2 in the following way:
string myname ="";
if (Request["myname"] != null)
myname = Request.QueryString["myname"];

Using Cookies:
Developers use cookies to store small amounts of information on the client. But keep in mind that user may refuse cookies, so you have to handle it. Finally i can say cookies are created on the server side but saved on the client side.

Now we create a cookie from page1 & read from page2:
for page1 button click event:
protected void cmdTransfer_Click(object sender, EventArgs e)
if(Request.Browser.Cookies) // To check that the browser support cookies
HttpCookie cookie = new HttpCookie("myname");
cookie.Value = txtName.Text;
cookie.Expires = DateTime.Now.AddDays(1);
// You have to follow other option.

Now read from page2:

if(Request.Cookies["myname"] != null)

Using Session Variables:
By default, session variables are stored in the webserver's memory. Session variables are unique per each user. If you open same url in two browser then server will create two independent sessions for you. Keep in mind that each session has a expire time. You can configure session time from IIS. Also one another thing is, you can receive session value form any pages after assigning. Write the below code under page1 button:
protected void cmdTransfer_Click(object sender, EventArgs e)
Session["myname"] = txtName.Text;

Now read session variable value from page2:
string myname ="";

Using Server.Transfer:
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. If the user clicks Refresh on his browser, the browser prompt a message warning that the page can not be regreshed without resending information. Write the below code under page1 button click event:
protected void cmdTransfer_Click(object sender, EventArgs e)
Server.Transfer("page2.aspx", true);

And write the below code in page2 load event:
if (Request.Form["txtEmail"])

Now run the page1, enter your name & click. You will see your name in page2.

Using Post Back URL:
ASP.NET 2.0 has been introduced Cross page posting. By PreviousPage object you can search controls value from which page you are redirected here. To implement Cross page posting you have to define the PostBackUrl property of page1 button to page2. Like:
<asp:Button id="cmdTransfer" runat="server" Text="Transfer" PostBackUrl="page2.aspx"/>

Now write the below code in page2 load event:
TextBox txtName = (TextBox)(PreviousPage.FindControl("txtName"));
string myname =txtName.Text;

So keep experimenting.


Anonymous said...

Thanks, this is very helpful!

Anonymous said...

Helpful indeed!
Tahnks for the quick recap.

Anonymous said...

Nice Examples with details

Anonymous said...

thanks! it is very helpful for beginners.

Anonymous said...

thanks! it is very helpful.

Anonymous said...

Nice way to describe all the way of transfering data from one page to another page- Vishal Sanchihar

Anonymous said...

usefull information

Anonymous said...

Thank you........ :)

Anonymous said...

thanx a tn....

Anonymous said...

Thank you so much, that was very helpful

Anonymous said...

its really very good and help me in solve my problem

Anonymous said...

thank you very much, you really helped me.

massimo tassisto said...

thank you this was really helpfull!!!

Anonymous said...

Thank you so much. Excellent tutorial.

Anonymous said...

very nice thanks....

iPad Application Development said...

This website is providing the lot of topics are visible in this blog that to valuable info in this blog.

Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash