Textbox Data Transfer One Page to Another Page Using Various ASP.NET Techniques

We will see techniques for TextBox value retrievals from one page to another page.

Technique 1: Data Transfer with Cookies

In this example the TextBox data transfer uses cookies as in the following.

Form 1

Design: Add FillInfo.aspx.

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="FillInfo.aspx.cs" Inherits="UI_Cookies_FillInfo" %>  
  2.   
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  6.       
  7.     <div>      
  8.         <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>  
  9.         <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>  
  10.         <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />  
  11.     </div>  
  12. </asp:Content>  
Code: FillInfo.aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_Cookies_FillInfo : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.     }  
  14.     protected void btnsend_Click(object sender, EventArgs e)  
  15.     {  
  16.         HttpCookie Cookie = new HttpCookie("Username");  
  17.         Cookie.Value = txtUsername.Text;  
  18.         Cookie.Expires = DateTime.Now.AddHours(1);  
  19.         Response.Cookies.Add(Cookie);  
  20.         Response.Redirect("ShowInfo.aspx");  
  21.     }  
  22. }  
Form 2

Design: Add ShowInfo.aspx.

Code: ShowInfo.aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_Cookies_ShowInfo : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         if (Request.Cookies["Username"] != null)  
  13.         {  
  14.             Response.Write(Request.Cookies["Username"].Value.ToString());  
  15.         }     
  16.     }  
  17. }  
Now run the FillInfo.aspx page and fill in the user name as in the following:

Fill Cookies value
Figure 1: Fill in Cookie value

Display Cookies value
Figure 2: Display Cookie value

Technique 2: Data Transfer with Application Variable

In this example the TextBox data transfer uses an application variable as in the following.

First create two forms.

From 1

Design:
FillApplicationVariable .aspx.
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="FillApplicationVariable .aspx.cs" Inherits="UI_Appvariable_FillApplicationVariable_" %>  
  2.   
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  6.   
  7.     <div>      
  8.         <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>  
  9.         <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>  
  10.         <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />  
  11.     </div>  
  12.   
  13. </asp:Content>  
Code: FillApplicationVariable .aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_Appvariable_FillApplicationVariable_ : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.     }  
  14.     protected void btnsend_Click(object sender, EventArgs e)  
  15.     {  
  16.         Application["Username"] = txtUsername.Text;  
  17.         Response.Redirect("ShowApplicationVariable .aspx");  
  18.     }  
  19. }  
Form 2

Design: Add ShowApplicationVariable .aspx.

Code: ShowApplicationVariable .aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_Appvariable_ShowApplicationVariable_ : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         if (Application["Username"] != null)  
  13.         {  
  14.             string Name = Application["Username"].ToString();  
  15.             Response.Write(Name);  
  16.         }  
  17.     }  
  18. }  
Now run the form in the browser and fill in the application variable's value in the TextBox as in the following.

Fill application variable value
Figure 3: Fill application variable value

Display Application variable value
Figure 4: Display application variable value

Technique 3: Data Transfer with Session Variable

In this example the TextBox data transfer uses a session variable as in the following.

Form 1

Design: Add FillSessionName.aspx.
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="FillSessionName.aspx.cs" Inherits="UI_Session_FillSessionName" %>  
  2.   
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  6.   
  7.     <div>      
  8.         <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>  
  9.         <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>  
  10.         <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />  
  11.     </div>  
  12.   
  13. </asp:Content>  
Code: FillSessionName.aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_Session_FillSessionName : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.     }  
  14.     protected void btnsend_Click(object sender, EventArgs e)  
  15.     {  
  16.         Session["Username"] = txtUsername.Text;  
  17.         Response.Redirect("ShowSessionName.aspx");  
  18.     }  
  19. }  
Form 2

Design:
Add ShowSessionName.aspx.

Code: ShowSessionName.aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_Session_ShowSessionName : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         if (Session["Username"] != null)  
  13.         {  
  14.             Response.Write(Session["Username"].ToString());  
  15.         }     
  16.     }  
  17. }  
Now run the form in a browser and fill in the session variable value in TextBox as in the following.

Fill Session Variable
Figure 5: Fill Session Variable

Display Session variable
Figure 6: Display Session variable

Technique 4: Data Transfer with Query String

In this example the TextBox data transfer in the next page of the browser URL path using a Query String is as in the following.

Form 1

Design: Add FillStringValue.aspx.
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="FillStringValue.aspx.cs" Inherits="UI_QueryString_FillStringValue" %>  
  2.   
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  6.   
  7.     <div>      
  8.         <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>  
  9.         <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>  
  10.         <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />  
  11.     </div>  
  12.   
  13. </asp:Content>  
Code: FillStringValue.aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_QueryString_FillStringValue : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.     }  
  14.     protected void btnsend_Click(object sender, EventArgs e)  
  15.     {  
  16.         Response.Redirect("ShowStringValue.aspx?Username=" + txtUsername.Text);  
  17.     }  
  18. }  
Form 2

Design: Add ShowStringValue.aspx.

Code: ShowStringValue.aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class UI_QueryString_ShowStringValue : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         string Name = Request.QueryString["Username"].ToString();  
  13.         Response.Write(Name);  
  14.     }  
  15. }  
Now run the form in a browser and fill in a string variable value into the TextBox as in the following.

Fill String value
Figure 7: Fill String value

Display String Value
Figure 8: Display String Value

I hope you like this article and understood how to pass TextBox values from one page to another page.

Up Next
    Ebook Download
    View all
    Learn
    View all