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:
我正在尝试使用c# ASP.Net将表单数据从一个页面发送到另一个页面。我默认有两页。aspx和default2.aspx。这是我在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?
据我所知,PostBackUrl用于设置要发送数据的页面,对吗?
Also how can I retrieve the data that is sent to Default2.aspx?
另外,如何检索发送到Default2.aspx的数据?
4 个解决方案
#1
20
You have a few options, consider
你有几个选择,考虑一下
- Session state
- 会话状态
- 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.
ASP。NET会话状态允许在用户导航ASP时为用户存储和检索值。Web应用程序中的NET页面。HTTP是无状态协议。这意味着Web服务器将每个HTTP请求作为独立的请求处理。服务器不知道在以前的请求中使用的变量值。ASP。NET会话状态在一个有限的时间窗口中标识来自同一浏览器的请求,作为会话,并提供了在会话期间持久化变量值的方法。默认情况下,ASP。所有ASP都启用了NET会话状态。网络应用程序。
Best of all, it is easy!
最重要的是,这很简单!
Put data in (for example on default1.aspx)
输入数据(例如,在default1.aspx上)
Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = LastNameTextBox.Text;
Get it out (for example on default2.aspx)
把它取出来(例如,在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.
如果您正在发送少量数据(如id=4),那么使用查询字符串变量可能更实用。
You should explore the use of the query string variables, e.g.
您应该研究查询字符串变量的用法,例如。
http://www.domain.com?param1=data1¶m2=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
如果您不熟悉querystring变量,请查看他们的wikipedia文章
#2
7
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:
Foe示例假设您的文本框包含登录凭据,然后将它们保存到会话中,以便您以后可以在任何其他页面中使用它们。是这样的:
In Button_Click-
Button_Click -
Session["name"]=TextBox1.Text;
Session["pwd"]= TextBox2.Text;
Instead of PostBackUrl="~/Default2.aspx"
you can write the following-
而不是PostBackUrl = " ~ / Default2。你可以写下面的-
//in button click
Server.Transfer("~/Default2.aspx");
In Default2.aspx page load:
在Default2。aspx页面加载:
string a= Session["name"].ToString();
string b= Session["pwd"].ToString();
#3
3
Try this in the Page_Load
of Default2.aspx.
在Default2.aspx的Page_Load中尝试此操作。
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.
是的,您是对的,如果您使用PostBackUrl属性,第1页的数据将被发送到第2页。
MSDN链接
#4
2
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 Session
s?
虽然这里所有的答案都是有效的,但有些不是最有效的。为什么一个简单/标准的http POST必须调用(昂贵的)服务器端会话?
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.
在设置PostBackUrl(如果内存服务于asp.net版本1)之前,服务器。传递和获取对前一页的引用是跨页发布是如何完成/记录的。但是,有了PostBackUrl,事情就回到了基本的东西——从一个资源到另一个资源的标准http POST。
Here's a similar SO thread that maybe helpful.
这里有一个类似的线程,可能会有帮助。
#1
20
You have a few options, consider
你有几个选择,考虑一下
- Session state
- 会话状态
- 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.
ASP。NET会话状态允许在用户导航ASP时为用户存储和检索值。Web应用程序中的NET页面。HTTP是无状态协议。这意味着Web服务器将每个HTTP请求作为独立的请求处理。服务器不知道在以前的请求中使用的变量值。ASP。NET会话状态在一个有限的时间窗口中标识来自同一浏览器的请求,作为会话,并提供了在会话期间持久化变量值的方法。默认情况下,ASP。所有ASP都启用了NET会话状态。网络应用程序。
Best of all, it is easy!
最重要的是,这很简单!
Put data in (for example on default1.aspx)
输入数据(例如,在default1.aspx上)
Session["FirstName"] = FirstNameTextBox.Text;
Session["LastName"] = LastNameTextBox.Text;
Get it out (for example on default2.aspx)
把它取出来(例如,在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.
如果您正在发送少量数据(如id=4),那么使用查询字符串变量可能更实用。
You should explore the use of the query string variables, e.g.
您应该研究查询字符串变量的用法,例如。
http://www.domain.com?param1=data1¶m2=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
如果您不熟悉querystring变量,请查看他们的wikipedia文章
#2
7
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:
Foe示例假设您的文本框包含登录凭据,然后将它们保存到会话中,以便您以后可以在任何其他页面中使用它们。是这样的:
In Button_Click-
Button_Click -
Session["name"]=TextBox1.Text;
Session["pwd"]= TextBox2.Text;
Instead of PostBackUrl="~/Default2.aspx"
you can write the following-
而不是PostBackUrl = " ~ / Default2。你可以写下面的-
//in button click
Server.Transfer("~/Default2.aspx");
In Default2.aspx page load:
在Default2。aspx页面加载:
string a= Session["name"].ToString();
string b= Session["pwd"].ToString();
#3
3
Try this in the Page_Load
of Default2.aspx.
在Default2.aspx的Page_Load中尝试此操作。
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.
是的,您是对的,如果您使用PostBackUrl属性,第1页的数据将被发送到第2页。
MSDN链接
#4
2
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 Session
s?
虽然这里所有的答案都是有效的,但有些不是最有效的。为什么一个简单/标准的http POST必须调用(昂贵的)服务器端会话?
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.
在设置PostBackUrl(如果内存服务于asp.net版本1)之前,服务器。传递和获取对前一页的引用是跨页发布是如何完成/记录的。但是,有了PostBackUrl,事情就回到了基本的东西——从一个资源到另一个资源的标准http POST。
Here's a similar SO thread that maybe helpful.
这里有一个类似的线程,可能会有帮助。