How to Pass Transfer Send data from one page to another Aspx page in Asp.Net C# VB.Net

How to send data from one page to another page in asp.net C# VB.Net

Send data from one aspx page to another aspx page is a very common need in any asp.net C# web project. Specially whenever you develop a project you have to redirect or navigate from one ASP.NET Web page to another, in these cases you will feel the need of sending data from one page to another page more often. In this post, I will show you some common & easy ways on how we can send data from one page to another page in Asp.net, those techniques are basically termed as State Management in programming language.  To demonstrate the ways in my below examples I will create 2 aspx pages. The first page consists of a text field and a button. Consider it as “page1”. And the second page is a blank 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:

1. Query Strings
2. Cookies
3. Session variables
4. Server.Transfer
5. Post Back URL

We have to use one of them to Send data from one page to another page in Asp.Net.

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

Using Query String to Send Data from one Page to another Page:

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 browser 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://codedisplay.com?var1=10&var2=20'>......</a>

Below is an example:
Suppose you want to pass the txtName TextBox value from page1 to page2 on cmdTransfer button click event then:
C# Code:

protected void cmdTransfer_Click(object sender, EventArgs e)
{
	Response.Redirect("page2.aspx?myname=" + txtName.Text);
}

VB.Net Code:

Protected Sub cmdTransfer_Click(sender As Object, e As EventArgs)
	Response.Redirect("page2.aspx?myname=" + txtName.Text)
End Sub

Now the question is how we can read/retrieve my name from page2.aspx page? The way is:
C# Code:

string myname = Request.QueryString["myname"];

VB.Net Code:

Dim myname As String = Request.QueryString("myname")

Now a bit advance, if you want to transfer special characters or a line with spaces then you must have to use Server.UrlEncode method before transfer and Server.UrlDecode method to read data from page2.aspx:

C# Code:

protected void cmdTransfer_Click(object sender, EventArgs e)
{
	Response.Redirect("page2.aspx?myname=" + Server.UrlEncode(txtName.Text));
}

VB.Net Code:

Protected Sub cmdTransfer_Click(sender As Object, e As EventArgs)
	Response.Redirect("page2.aspx?myname=" & Server.UrlEncode(txtName.Text))
End Sub

And to read from page2:
C# Code:

string myname = Server.UrlDecode(Request.QueryString["myname"]);

VB.Net Code:

Dim myname As String = Server.UrlDecode(Request.QueryString("myname"))

Now i want to show you how you can transfer more data:
C# Code:

protected void cmdTransfer_Click(object sender, EventArgs e)
{
	Response.Redirect("page2.aspx?myname="+Server.UrlEncode(txtName.Text)+"&myage="+txtAge.Text);
}

VB.Net Code:

Protected Sub cmdTransfer_Click(sender As Object, e As EventArgs)
	Response.Redirect(("page2.aspx?myname=" & Server.UrlEncode(txtName.Text) & "&myage=") + txtAge.Text)
End Sub

Now read both name & age from page2 in the same way:
C# Code:

string myname = Request.QueryString["myname"];
string myage = Request.QueryString["myage"];

VB.Net Code:

Dim myname As String = Request.QueryString("myname")
Dim myage As String = Request.QueryString("myage")

Now i want to show you how you can detect whether a parameter contains value or not. So we have to modify read data section in page2 in the following way:

C# Code:

string myname ="";
if (Request["myname"] != null)
	myname = Request.QueryString["myname"];

VB.Net Code:

Dim myname As String = ""
If Request("myname") IsNot Nothing Then
	myname = Request.QueryString("myname")
End If

Using Cookie to Send Data from one Page to another Page:

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 and read from page2:
For page1 button click event:

C# Code:

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.
	}
}

VB.Net Code:

Protected Sub cmdTransfer_Click(sender As Object, e As EventArgs)
	If Request.Browser.Cookies Then
		' To check that the browser support cookies
		Dim cookie As 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.
	End If
End Sub

Now read cookies from page2:
C# Code:

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

VB.Net Code:

If Request.Cookies("myname") IsNot Nothing Then
	Response.Write(Request.Cookies("myname").Value)
End If

Using Session Variable to Send Data from one Page to another Page:

By default, session variables are stored in the web server’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 click event:

C# Code:

protected void cmdTransfer_Click(object sender, EventArgs e)
{
	Session["myname"] = txtName.Text;
	Response.Redirect("page2.aspx");
}

VB.Net Code:

Protected Sub cmdTransfer_Click(sender As Object, e As EventArgs)
	Session("myname") = txtName.Text
	Response.Redirect("page2.aspx")
End Sub

Now read session variable value from page2:
C# COde:

string myname ="";
if(Session["myname"]!=null)
	myname=Session["myname"].ToString();

VB.Net Code:

Dim myname As String = ""
If Session("myname") IsNot Nothing Then
	myname = Session("myname").ToString()
End If

Using Server.Transfer to Send Data from one Page to another Page:

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 refreshed without re sending information. Write the below code under page1 button click event:

C# Code:

protected void cmdTransfer_Click(object sender, EventArgs e)
{
	Server.Transfer("page2.aspx", true);
}

VB.Net Code:

Protected Sub cmdTransfer_Click(sender As Object, e As EventArgs)
	Server.Transfer("page2.aspx", True)
End Sub

And write the below code in page2 load event:
C# Code:

if (Request.Form["txtEmail"])
	Response.Write(Request.Form["txtNamel"]);

VB.Net Code:

If Request.Form("txtEmail") Then
	Response.Write(Request.Form("txtNamel"))
End If

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

Using Post Back URL to Transfer Data:

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:
C# Code:

TextBox txtName = (TextBox)(PreviousPage.FindControl("txtName"));
string myname =txtName.Text;

VB.Net Code:

Dim txtName As TextBox = DirectCast(PreviousPage.FindControl("txtName"), TextBox)
Dim myname As String = txtName.Text

Here i have tried my best to explain how to send data from one page to another in asp.net using C# & VB.net code with example. If you have any other best way(s) please do comment.

Posted in .Net, Asp.net, Asp.net C# Vb.net Interview Question, C#, VB.Net
12 comments on “How to Pass Transfer Send data from one page to another Aspx page in Asp.Net C# VB.Net
  1. PRASHANT says:

    SUPERB EXPLAINATION…SIMPLE AND FINE EXPLAIN ..

  2. Malik Jehangir says:

    absolutely amazing

  3. Ankush Mahajan says:

    really help me…. excellent…

  4. Anita says:

    Excellent work…

  5. parth says:

    create object one page and value pass in second page in vb.net code

  6. sarvan says:

    I don’t find my data in one aspx to another aspx ..I can login but data could not login

  7. senthil murugan says:

    really help me…. excellent…

  8. JeremyC says:

    Mine says Null Object Reference in the next page :/

  9. jai says:

    when i double click the textbox of webform1.its should open a popupwindow. there i should get the value for textbox . but when run the program and double the textbox its opening the another webfrom as popup window there only its redirecting & getting value , its not closing .and not getting value to its parent browser can any 1 slove this issue how can i get values from popup window and its must to close and alue must to come in parent window

    • code display says:

      HI, Thanks for your comment. The below post may help you. http://codedisplay.com/automaticaly-refresh-parent-window-whenever-popup-window-is-closed/

  10. Shobana says:

    Really Helpfull

  11. Shobana says:

    Really helpful

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*