Data Transfer between two ASP.NET Pages

In most of the present day Web Applications, it is required to transfer data from one web page to another web page. Requirements like maintaining user preferences across the application, identifying user between transactions, correlating data based on previous page options etc, always drive programmers to write code to transfer data from page to page.

There are numerous ways in accomplishing the above said tasks, selecting an appropriate way for an appropriate situation is vested in the hands of programmer. Some of the most prominent techniques, out of my knowledge are listed in this article.

Data Transfer Techniques:

  1. QueryString
  2. PreviousPage.FindControl() and Request.Form[]
  3. Session State
  4. Cookies
  5. Application Variables
  6. Context.Items[]

Page A —> Transfer Data —> Page B

1) Using QueryString:

  • A query string is information that is appended to the end of a page URL.
  • Query strings provide a simple but limited way to maintain state information.
  • Do not rely on query strings to convey important or sensitive data, because data can be tampered by a user at any point. And also a user can bookmark the URL and thereby the information in it.

Page A:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("B.aspx?nameA="+TextBox1.Text);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string nameB = Request.QueryString["nameA"].ToString();
        Response.Write(nameB);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>

2) Using PreviousPage.FindControl():

  • This technique is much more helpful when it comes to Cross Page postback previous page data access.
  • When we use Tranfer method or cross page postback, this FindControl() method is helpful in getting the PreviousPage data, but if we request a page directly, then PreviousPage renders null reference.
  • Even Request.Form[“Control ID”] can also be used to retrieve previous page data when transfer method is used (especially when the two pages are in different applications).
  • When we want to find control inside a template, then we need to refer the container containing the control at first and then we should find control.
  • Using with Master pages, first we need to find the content place holder in the master page and then we got to find the actual control in the content place holder (Ex: ContentPlaceHolder cph = (ContentPlaceHolder)Page.PreviousPage. FindControl("contentplaceholder1");).

Page A:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.PreviousPage != null)
        {
            TextBox tb = (TextBox)Page.PreviousPage.FindControl("TextBox1");
            if (tb != null)
            {
                Response.Write(tb.Text);
            }
        }        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>

3) Using Session State:

  • This is a server side state management technique. It main advantages are Data persistence, simple implementation, platform scalability, cookieless support.
  • The main drawback of this method is it reduces the application performance. The more the data in the session and more number of sessions can degrade the application performance by consuming more server memory. This can be avoided by removing session variables from time to time using Session.Remove(“variable name”).

Page A:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["nameA"] = TextBox1.Text;
        Response.Redirect("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["nameA"] != null)
        {
            Response.Write(Session["nameA"].ToString());
        }               
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>

4) Using Cookies:

  • The main advantages of transferring data using cookies are server resources are not required, data persistence, simplicity and flexible configuration expiration rules.
  • The disadvantages of cookies are its size limitations, high chance of getting tampered at client side.

Page A:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie Cookie = new HttpCookie("nameA");
        Cookie.Value = TextBox1.Text;
        Cookie.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(Cookie);
        Response.Redirect("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["nameA"] != null)
        {
            Response.Write(Request.Cookies["nameA"].Value.ToString());
        }               
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>

5) Using Application Variables:

  • The main advantage of using application variables to transfer data is the data available throughout the application. And also these are simple to use.
  • The disadvantages of application variables are consumption of resources, limited durability and even global scope is also a drawback in certain cases.

Page A:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Application["nameA"] = TextBox1.Text;
        Response.Redirect("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Application["nameA"] != null)
        {
            string nameB = Application["nameA"].ToString();
            Response.Write(nameB);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>

6) Using Context:

  • The Context.Items collection is also capable of storing objects instead of just plain character data.
  • Context.Items only last for current request and once the page postbacks the data in it will be lost, this is a required feature in many applications.

Page A:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Context.Items.Add("nameA",TextBox1.Text);
        Server.Transfer("B.aspx");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Label ID="Label1" runat="server" Text="Enter name : "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Transfer Data" 
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Page B:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
            string nameB = Context.Items["nameA"].ToString();
            Response.Write(nameB);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Page B</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    </div>
    </form>
</body>
</html>

Conclusion:

Different ways to pass data between two web pages are discussed. Practical implementation with Response.Redirect() and Server.Transfer() has been done. The advantages and disadvantages of all the practical methods are discussed.

You may also like...

One Pingback/Trackback

    22 July 2011 at 7:07am
    You got a really useful blog... I have been here reading for about half ...
  • Raghav

    Nice one!!!

  • well written blog. Im glad that I could find more info on this. thanks

  • Keep posting stuff like this i really like it

  • bob taylor

    Excellent article! I’m definitely a new comer in the ASP.NET world. Still deciding which to focus on; VB or C#. Leaning towards C#.

  • Arun

    Really nice way & clear way to write a blog & explain things so precisely. thanks very very much. plz do keep posting articles like that. it helped me a lot coz im a new to .net framework.
    thanks

  • sarathy

    Nice article

  • Jayesh

    Great Dear

    Excellent information.

    God Bless U everytime.

  • Jyothi

    Nice one…….

  • Alaa Yosef

    thaaaaaaaaaaaaaaaaanks Excellent article clear ,simple ,realy thanks alooooot.

  • vendan

    Excellent article, Thanks for your service…

  • SAJEED S. M.

    easily understandable article!!

  • mohit pandey

    NICE.. thanks a lot

  • Pingback: Anonymous()

  • akhilesh gupta

    Thanks a lot ………
    for this article…………

  • Very nicely compiled

    Very nicely compiled

  • ashi

    not working
    2.PreviousPage.FindControl() and Request.Form[]

    i tried it more than one time but not working
    i am using a master page and more than one content page
    and i want that find the value from one content page to another page

  • RamiVemula

    @Ashi – In case you have Master Page, you need to recursively use FindControl to get the actual control, or else you will not be able to find the actual control in content page.

  • MadhuKrishna

    How to retrieve the public property data in next page, I mean after redirecting to next page?
    for example:
    I have 2 pages. 1. Default.aspx
    2nd one is NextPage.aspx
    I am setting a property in Default.aspx page I wanna retrieve in NextPage.aspx aftr redirecting to NextPage.aspx by redirecting Response.redirect(“NextPage.aspx”);

  • RamiVemula

    @MadhuKrishna – you can go with Server.Transfer() and use PreviousPage property to retrieve the property of your interest.

  • yogendra

    Thanks a lot
    for this article

  • santosh

    Great work Thanks..

  • Ayush

    Very nice article.
    Thanks

  • Nilesh Jha

    Thanks.
    Here you had mention very good example.

  • prasad bhagat

    very nice article,
    keep posting
    thank you very much ..

  • Yohan Apriandi

    very nice article,
    keep posting.
    thank you very much ..

    Great work