How to Pass or Transfer Data or Parameters values from one Aspx page to another Aspx page in Asp.Net C# VB.Net

Pass or Transfer data from one page to another page is a very common task in Web development. Specially when you develop a project, for redirects or navigates from one ASP.NET Web page to another, you will frequently need to pass information from the source page to the target page or destination page. In this post, I will show you some ways of transferring data or parameters values between one page to another page which is 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:

1. Query Strings
2. Cookies
3. Session variables
4. Server.Transfer
5. 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 to Transfer Data:
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 does 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 Transfer Data:
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 Transfer Data:
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 Transfer Data:
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

So keep experimenting.

Posted in .Net, Asp.net, Asp.net C# Vb.net Interview Question, C#, VB.Net
6 comments on “How to Pass or Transfer Data or Parameters values from one Aspx 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

Leave a Reply

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

     

*