Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

am working on asp.net C#

How to pass values from one page to another page.

I have 2 pages in my project

In first page i have 3 textboxes and a submit button

In second page i have 3 labels

what ever i enter in first page 3 textboxes and click on submit button, these 3 values must display in second page labels.

Please help how to do this.

Thanks
Posted

There are three ways you can do it:
1) Query string: wwww.mydomain.com/newpage.apsx?TB1Value='hello'&TB2Value='goodbye'
2) Cookie - this is stored on the client and can be long lasting
3) Session - this is stored on the server, and will be removed as soon as the session ends.

Which to use is up to you: but Google and MSDN can help you decide and show you how to implement them - they are all very easy!
 
Share this answer
 
Comments
Karthik_Mahalingam 4-Jan-14 4:57am    
Griff you are so fast :)
You need to use session variables. In the first page, in the button click method:
C#
protected void yourButton_Click(object sender, EventArgs e)
{
    Session["txtBox1"] = yourFirstTextBox.Text;
    Session["txtBox2"] = yourSecondTextBox.Text;
    Session["txtBox3"] = yourThirdTextBox.Text;
    Response.Redirect("SecondPage.aspx");
}

And in the second page, add this in the Page_Load method:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["txtBox1"] != null)
    {
        yourFirstLabel.Text = (string)Session["txtBox1"];
    }
    else
    {
         // the session value "txtBox1" is null
    }
    // do this for all labels
}

More about ASP.NET sessions:
Exploring Session in ASP.NET[^]
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 4-Jan-14 5:02am    
good.
Thomas Daniels 4-Jan-14 5:03am    
Thank you!
on top of griff,programfox,sandeep solution
another live example using cookies.

C#
// in page1.aspx , button click
           HttpCookie mycookie = new HttpCookie("mycookie");
           mycookie["text1"] = text1.Text;
           mycookie["text2"] = text2.Text;
           mycookie["text2"] = text3.Text;
           Response.Cookies.Add(mycookie);
           Response.Redirect("page2.aspx");

           // in page2.aspx page load
           HttpCookie mycookie = Request.Cookies["mycookie"];
           if (mycookie != null)
           {
               label1.Text = mycookie["text1"];
               label2.Text = mycookie["text2"];
               label3.Text = mycookie["text3"];
           }
 
Share this answer
 
Page 1

.aspx Page

HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
    <asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
    <asp:TextBox ID="txtFirstName" runat="server" ></asp:TextBox><br />
    <asp:Label ID="lblMiddleName" runat="server" Text="Middle Name"></asp:Label>
    <asp:TextBox ID="txtMiddleName" runat="server" ></asp:TextBox><br />
    <asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
    <asp:TextBox ID="txtLastName" runat="server" ></asp:TextBox><br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit"
        onclick="btnSubmit_Click" />
</form>
</body>
</html>



.cs Page

C#
using System;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Session["FirstName"] = txtFirstName.Text;
            Session["MiddleName"] = txtMiddleName.Text;
            Session["LastName"] = txtLastName.Text;
            Response.Redirect("WebForm2.aspx");
        }

    }
}



Page 2

.aspx page

HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label><br />
        <asp:Label ID="lblMiddleName" runat="server" Text="Middle Name"></asp:Label><br />
        <asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
    </div>
    </form>
</body>
</html>




.cs page

C#
using System;

namespace WebApplication2
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFirstName.Text = Session["FirstName"].ToString();
            lblMiddleName.Text = Session["MiddleName"].ToString();
            lblLastName.Text = Session["LastName"].ToString();
        }
    }
}
 
Share this answer
 
Comments
Karthik_Mahalingam 4-Jan-14 5:07am    
sandeep,
ProgramFOX had already posted using session variables,
you could have posted using query string..
good try..
Sandeep Singh Shekhawat 4-Jan-14 6:28am    
Thanks Karthik
Karthik_Mahalingam 4-Jan-14 6:33am    
welcome sandeep :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900