Send data from one page to another

I am trying to send form data from one page to another using C# ASP.Net. I have two pages default.aspx and default2.aspx.Here is the code I have in default.aspx:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Go" 
    PostBackUrl="~/Default2.aspx" />
<br />

From what I know so far the PostBackUrl is used to set the page in which you want the data to be sent is this correct?

Also how can I retrieve the data that is sent to Default2.aspx?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have a few options, consider

  1. Session state
  2. Query string

Session state

If you are going to send data between pages, you could consider the use of Session State.

ASP.NET session state enables you to store and retrieve values for a
user as the user navigates ASP.NET pages in a Web application. HTTP is
a stateless protocol. This means that a Web server treats each HTTP
request for a page as an independent request. The server retains no
knowledge of variable values that were used during previous requests.
ASP.NET session state identifies requests from the same browser during
a limited time window as a session, and provides a way to persist
variable values for the duration of that session. By default, ASP.NET
session state is enabled for all ASP.NET applications.

Best of all, it is easy!

Put data in (for example on default1.aspx)

Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = LastNameTextBox.Text;

Get it out (for example on default2.aspx)
string firstname = Session["FirstName"] // value of FirstNameTextBox.Text;
string lastname = Session["LastName"] // value of LastNameTextBox.Text;

Query string

If you are sending small amounts of data (eg id=4), it may be more practical to use query string variables.

You should explore the use of the query string variables, e.g.

You can then get the data out like
string param1 = Request.QueryString["param1"]; // value will be data1
string param2 = Request.QueryString["param2"]; // value will be data2

You can use something like How do you test your Request.QueryString[] variables? to get the data out.

If you are unfamiliar with querystring variables check out their wikipedia article

Method 2

Session variables can be useful in this context.

Foe example suppose your textboxes contain login credentials, then save them in sessions so that you can later use them in any other page. Like this:

In Button_Click-

Session["pwd"]= TextBox2.Text;

Instead of PostBackUrl="~/Default2.aspx" you can write the following-
//in button click

In Default2.aspx page load:
string a= Session["name"].ToString();
string b= Session["pwd"].ToString();

Method 3

Try this in the Page_Load of Default2.aspx.

 if (PreviousPage != null)
            if (((TextBox)PreviousPage.FindControl("TextBox1")) != null)
                string txtBox1 = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;

And yes you are correct, the data from page 1 will be sent to page 2 if you use the PostBackUrl attribute.

MSDN link

Method 4

While all the answers here will work some aren’t the most efficient. Why would a simple/standard http POST have to invoke (expensive) server-side Sessions?

Your code isn’t doing anything special – it is simply POSTing a form to another page. All you need to do to obtain the POSTed data is go through the Request.Form collection.

Prior to the availability to set the PostBackUrl (if memory serves version 1 of, Server.Transfer and getting references to the previous page was how cross-page POSTing was done/documented. However, with PostBackUrl, things go back to basics, the way it should be – a standard http POST from one resource to another.

Here’s a similar SO thread that maybe helpful.

Method 5

another way is to save the data on a database and fetch it back on the other page:

    string query = "UPDATE table SET col = 'a'";
    SqlCommand command = new SqlCommand(query, connection);

    string query = "SELECT col FROM table";
    SqlCommand command = new SqlCommand(query, connection);
    string value = command.ExecuteScalar();

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x