Please visit my new Web Site https://coderstechzone.com
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
*** CookiesAccording 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
*** 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:
Below is an example:
Suppose you want to pass the txtName TextBox value from page1 to page2 on cmdTransfer button click event:
Now the question is how we can read/retrieve my name from page2.aspx page? The way is:
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:
and to read from page2:
Now i want to show you how you can transfer more data:
Now read both name & age from page2 in the same way:
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:
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:
Now read from page2:
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:
Now read session variable value from page2:
Using Server.Transfer: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);
}
{
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));
}
{
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);
}
{
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"];
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"];
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.
}
}
{
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);
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");
}
{
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();
if(Session["myname"]!=null)
myname=Session["myname"].ToString();
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);
}
{
Server.Transfer("page2.aspx", true);
}
And write the below code in page2 load event:
if (Request.Form["txtEmail"])
Response.Write(Request.Form["txtNamel"]);
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;
string myname =txtName.Text;
So keep experimenting.
16 comments:
Thanks, this is very helpful!
Helpful indeed!
Tahnks for the quick recap.
Nice Examples with details
Thanks
thanks! it is very helpful for beginners.
thanks! it is very helpful.
Nice way to describe all the way of transfering data from one page to another page- Vishal Sanchihar
usefull information
Thank you........ :)
thanx a tn....
Thank you so much, that was very helpful
its really very good and help me in solve my problem
thanks
thank you very much, you really helped me.
thank you this was really helpfull!!!
Thank you so much. Excellent tutorial.
very nice thanks....
This website is providing the lot of topics are visible in this blog that to valuable info in this blog.
I WOULD BE DELIGHTED TO HEAR FROM YOU