Sending 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 that I have by 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, PostBackUrl is used to set the page to which you want to send data, is this correct?

Also how can I get the data sent to Default2.aspx?

+6
source share
5 answers

You have several options, consider

  1. Session Status
  2. Query string

Session Status

If you intend to send data between pages, you might consider using Session State .

An ASP.NET session state allows you to store and retrieve values ​​for a user as the user navigates through ASP.NET pages in a web application. HTTP is a stateless protocol. This means that the web server processes each HTTP request per page as a standalone request. The server does not retain knowledge of the values ​​of the variables that were used during previous queries. An ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to store 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's easy!

Put the data (e.g. in default1.aspx)

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

Get it (e.g. on default2.aspx)

 string firstname = Session["FirstName"] // value of FirstNameTextBox.Text; string lastname = Session["LastName"] // value of LastNameTextBox.Text; 

Query string

If you send small amounts of data (e.g. id = 4), it may be more practical to use query string variables.

You should study the use of query string variables like

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

Then you can get the data as

 string param1 = Request.QueryString["param1"]; // value will be data1 string param2 = Request.QueryString["param2"]; // value will be data2 

You can use something like How to check Request.QueryString [] variables? to get the data.

If you are not familiar with query string variables, check out their Wikipedia article.

+23
source

Session variables may be useful in this context .

For example, suppose your text fields contain login credentials and then store them in sessions so that you can later use them on 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 loading the page Default2.aspx:

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

Try this in Page_Load Default2.aspx.

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

And yes, you're right, the data from page 1 will be sent to page 2 if you use the PostBackUrl attribute.

MSDN Link

+3
source

Although all the answers here will work, some of them are not the most effective. Why should a simple / standard http POST call the (expensive) server side of Session s?

Your code does nothing special - it's just POSTing the form to another page. All you have to do to get the POSTed data is to go through the Request.Form collection.

Before installing PostBackUrl (if the memory PostBackUrl asp.net version 1), Server.Transfer and getting links to the previous page showed how the interstitial POSTING is executed / documented. However, with PostBackUrl everything returns to the basics, as it should be - standard http POST from one resource to another.

Here's a similar SO stream that might be useful.

+2
source

Another way is to save the data in the database and retrieve it on another 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(); 
0
source

Source: https://habr.com/ru/post/1438251/


All Articles