ASP.NET is a web programming platform developed by Microsoft. It is the successor to Active Server Pages. The term "classic ASP" is often used to distinguish previous versions of Active Server Pages with the .NET (pronounced "dot net") versions. How many ways do you know to send the data between ASP.NET Pages? In this post I’m going to list 8 different ways.

Now, let me explain how to learn multiple ways to transfer data from one ASP.NET page to another. Some of these include using the cache, http posts, querystring variables, ASP.NET session state, etc. Each button in the demo has the required code in it's Click event handler, and brings you to a separate target page that gets and displays the data that was sent. Let’s check here are the methods:

1. Use the querystring:
The QueryString collection is used to retrieve the variable values in the HTTP query string.  Using this technique I will add my data with URL and on the next page will grab it.

 protected void QueryStringButton_Click(object sender, EventArgs e)
          {
              Response.Redirect("QueryStringPage.aspx?Data=" + Server.UrlEncode(DataToSendTextBox.Text));
         }


2. Use HTTP POST:
The Hypertext Transfer Protocol (HTTP) is a communication protocol that is designed to enable request-response between clients and servers.  Using this technique I will call a post back url and the on next page using Request.From I will grab it.

 <asp:Button ID="HttpPostButton" runat="server" Text="Use HttpPost" 
              PostBackUrl="~/HttpPostPage.aspx" onclick="HttpPostButton_Click"/>
 
 protected void HttpPostButton_Click(object sender, EventArgs e)
         {
      // The PostBackUrl property of the Button takes care of where to send it!
         }


3. Use Session State:
Sessions can be used to store even complex data for the user just like cookies. Actually, sessions will use cookies to store the data, unless you explicitly tell it not to. Sessions can be used easily in ASP.NET with the Session object. Using this technique I will store the data in session variable on the client machine and on the next page will grab it. Using Application Variable instead of Session Variable is recommended by experts.

    protected void SessionStateButton_Click(object sender, EventArgs e)
          {
              Session["Data"] = DataToSendTextBox.Text;
              Response.Redirect("SessionStatePage.aspx");
         }


4.  Use public properties:
Properties are not just to provide access to the fields; rather, they are supposed to provide controlled access to the fields of our class. Using this technique I will send the using a public method and on the next page will grab it using PreviousPage.MethodName.

    public string DataToSend
         {
             get
             {
                  return DataToSendTextBox.Text;
              }
         }
 
         protected void PublicPropertiesButton_Click(object sender, EventArgs e)
          {
              Server.Transfer("PublicPropertiesPage.aspx");
         }


5. Use PreviousPage Control Info:
Using this technique I will just redirect the user on next page and on the next page will use PreviousPage.FindControl to grab the data.

 protected void ControlInfoButton_Click(object sender, EventArgs e)
          {
              Server.Transfer("ControlInfoPage.aspx");
         }
 
   // target page:
 protected void Page_Load(object sender, EventArgs e)
         {
             var textbox = PreviousPage.FindControl("DataToSendTextbox") as TextBox;
              if (textbox != null)
             {
                 DataReceivedLabel.Text = textbox.Text;
             }
         }


6. Use HttpContext Items Collection:

   protected void HttpContextButton_Click(object sender, EventArgs e)
          {
              HttpContext.Current.Items["data"] = DataToSendTextBox.Text;
              Server.Transfer("HttpContextItemsPage.aspx");
         }
 
 // target page:
 protected void Page_Load(object sender, EventArgs e)
          {
              this.DataReceivedLabel.Text =(String) HttpContext.Current.Items["data"];
         }


7. Use Cookies:
Cookies are small pieces of text that are passed between browser and web server with every request. As a consequence, their values are available to any page within the site. Cookies are commonly used to store user preferences which help the site remember which features to turn on or off, for example. They might be used to record the fact that the current user has authenticated and is allowed to access restricted areas of the site. It is the browser's job to store persistent cookies as text files on the client machine. The storage duration is determined by the type of cookie and the expiry date that it is given. Cookies with no expiry date are not stored on the client machine and are cleared at the end of the user's session.

 protected void CookiesButton_Click(object sender, EventArgs e)
         {
             HttpCookie cook =  new HttpCookie("data");
             cook.Expires = DateTime.Now.AddDays(1);
             cook.Value = DataToSendTextBox.Text;
              Response.Cookies.Add(cook);
              Response.Redirect("HttpCookiePage.aspx");
         }
 
 // target page:
 protected void Page_Load(object sender, EventArgs e)
         {
             DataReceivedLabel.Text = Request.Cookies["data"].Value;
         }


8. Use Cache:
The Cache is primarily intended to be used to improve performance of web pages, in that you can add any arbitrary object to it and retrieve it at will. Cache items are stored in memory on the server, and can be considered as special global variables. In fact, pre-ASP.NET, Application state used to be treated as a kind of cache. The difference between Application variables and Cache is that Cache offers a management API. In Web Pages, cache is managed via a WebCache helper. When you want to add something to the Cache, you use the WebCache.Set method. This method takes four parameters: the name you want to give the cache item, the item itself, the number of minutes that the item should remain in the Cache (default: 20) and whether those minutes should restart everytime the item is referenced or not (default: true). This is called Sliding Expiration.

 protected void CacheButton_Click(object sender, EventArgs e)
          {
              Cache["data"] = DataToSendTextBox.Text;
              Server.Transfer("CachePage.aspx");
         }
    // target page:
     protected void Page_Load(object sender, EventArgs e)
          {
              this.DataReceivedLabel.Text = (string) Cache["data"];
         }