user1525474
user1525474

Reputation: 295

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?

Upvotes: 8

Views: 95405

Answers (5)

dizad87
dizad87

Reputation: 488

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

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

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

Upvotes: 1

Ian G
Ian G

Reputation: 30224

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.

http://www.domain.com?param1=data1&param2=data2

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

Upvotes: 26

Shiridish
Shiridish

Reputation: 4962

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["name"]=TextBox1.Text;
Session["pwd"]= TextBox2.Text;

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

//in button click
Server.Transfer("~/Default2.aspx");

In Default2.aspx page load:

string a= Session["name"].ToString();
string b= Session["pwd"].ToString();

Upvotes: 7

EdSF
EdSF

Reputation: 12371

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 asp.net), 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.

Upvotes: 2

Thousand
Thousand

Reputation: 6638

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;
                Response.Write(txtBox1);
            }
        }

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

MSDN link

Upvotes: 3

Related Questions