假设我在一页中有两个文本字段,一个字段用于名称,另一个是年龄的文本字段。

当我单击“提交”按钮时,这些值应显示在另一个页面中。任何人都可以举一个例子。.我完全感到困惑。

请帮我谢谢

有帮助吗?

解决方案

MSDN上有一个页面, 如何:在ASP.NET网页之间传递值:

即使源页在目标页面中的不同ASP.NET Web应用程序中,或者源页面不是ASP.NET网页,即使源页在不同的ASP.NET Web应用程序中,也可以使用以下选项:

  • 使用查询字符串。
  • 从源页面获取HTTP帖子信息。

仅当源和目标页面在同一ASP.NET Web应用程序中时,以下选项可用。

  • 使用会话状态。
  • 在源页面中创建公共属性,并在目标页面中访问属性值。
  • 从“源”页面中的控件中获取目标页面中的控制信息。

对于您的情况,听起来像是使用帖子是必经之路,因为您在第一页上有文本框。例子:

第一页:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" 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>Page 1</title>
</head>
<body>
  <form id="form1" runat="server" action="WebForm2.aspx">
  <div>
    Name: <asp:TextBox ID="tbName" runat="server"></asp:TextBox><br />
    Age: <asp:TextBox ID="tbAge" runat="server"></asp:TextBox><br />
    <asp:Button ID="submit" runat="server" Text="Go!" />
  </div>
  </form>
</body>
</html>

注意 action="WebForm2.aspx" 将帖子引导到第二页。没有代码。

第2页(接收页):

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication2.WebForm2" EnableViewStateMac="false" %>

<!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>Page 2</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Literal ID="litText" runat="server"></asp:Literal>
    </form>
</body>
</html>

注意 EnableViewStateMac="false" 页面元素上的属性。这一点很重要。

代码,使用简单的 Request.Form():

Public Class WebForm2
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    litText.Text = Request.Form("tbName") & ": " & Request.Form("tbAge")
  End Sub
End Class

那应该有效... :)

其他提示

将此代码放在您的提交按钮事件处理程序中,

private void btnsubmit_click(object sender,system.eventargs e){wendesp.redirect(“ elesepage.aspx?name =” + this.txtname.text +“&age =” + this.txtage.text); }

将此代码放到第二页page_load,

private void page_load(对象发送者,system.eventargs e){this.txtbox1.text = request.queryString [“ name”]; this.txtbox2.text = request.querystring [“ age”]; }

您有几个选择。 **

1.使用查询字符串。


(Cons)
  - Text might be too lengthy 
  - You might have to encrypt/decrypt query string based on your requirement 
(Pros)
  - Easy to manage

2.使用会话

(Cons)
  - May increase processing load on server
  - You have to check and clear the session if there are too many transactions
(Pros) 
  - Values from different pages, methods can be stored once in the session and retrieved from when needed
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top