Thursday, 28 March 2013

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


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


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='http://shawpnendu.blogspot.com?var1=10&var2=10'>......</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)
{
Response.Redirect("page2.aspx?myname="+Server.UrlEncode(txtName.Text)+"&myage="+txtAge.Text);
}

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.

Example:
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);
Response.Cookies.Add(cookie);
Response.Redirect("page2.aspx");
}
else
{
// You have to follow other option.
}
}

Now read from page2:

if(Request.Cookies["myname"] != null)
Response.Write(Request.Cookies["myname"].Value);

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;
Response.Redirect("page2.aspx");
}

Now read session variable value from page2:
string myname ="";
if(Session["myname"]!=null)
myname=Session["myname"].ToString();
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"])
Response.Write(Request.Form["txtNamel"]);

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;

No comments:

Post a Comment