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. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5. <div>
  6. <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>
  7. <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
  8. <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />
  9. </div>
  10. </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. public partial class UI_Cookies_FillInfo : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void btnsend_Click(object sender, EventArgs e)
  13. {
  14. HttpCookie Cookie = new HttpCookie("Username");
  15. Cookie.Value = txtUsername.Text;
  16. Cookie.Expires = DateTime.Now.AddHours(1);
  17. Response.Cookies.Add(Cookie);
  18. Response.Redirect("ShowInfo.aspx");
  19. }
  20. }
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. public partial class UI_Cookies_ShowInfo : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. if (Request.Cookies["Username"] != null)
  12. {
  13. Response.Write(Request.Cookies["Username"].Value.ToString());
  14. }
  15. }
  16. }
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. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5. <div>
  6. <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>
  7. <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
  8. <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />
  9. </div>
  10. </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. public partial class UI_Appvariable_FillApplicationVariable_ : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void btnsend_Click(object sender, EventArgs e)
  13. {
  14. Application["Username"] = txtUsername.Text;
  15. Response.Redirect("ShowApplicationVariable .aspx");
  16. }
  17. }
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. public partial class UI_Appvariable_ShowApplicationVariable_ : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. if (Application["Username"] != null)
  12. {
  13. string Name = Application["Username"].ToString();
  14. Response.Write(Name);
  15. }
  16. }
  17. }
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. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5. <div>
  6. <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>
  7. <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
  8. <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />
  9. </div>
  10. </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. public partial class UI_Session_FillSessionName : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void btnsend_Click(object sender, EventArgs e)
  13. {
  14. Session["Username"] = txtUsername.Text;
  15. Response.Redirect("ShowSessionName.aspx");
  16. }
  17. }
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. public partial class UI_Session_ShowSessionName : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. if (Session["Username"] != null)
  12. {
  13. Response.Write(Session["Username"].ToString());
  14. }
  15. }
  16. }
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. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5. <div>
  6. <asp:Label ID="Label1" runat="server" Text="User name : "></asp:Label>
  7. <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
  8. <asp:Button ID="btnsend" runat="server" Text="Send Text Value Next Page" OnClick="btnsend_Click" />
  9. </div>
  10. </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. public partial class UI_QueryString_FillStringValue : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void btnsend_Click(object sender, EventArgs e)
  13. {
  14. Response.Redirect("ShowStringValue.aspx?Username=" + txtUsername.Text);
  15. }
  16. }
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. public partial class UI_QueryString_ShowStringValue : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. string Name = Request.QueryString["Username"].ToString();
  12. Response.Write(Name);
  13. }
  14. }
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.