如何使用vb在asp.net中从一个页面到另一个页面获取值

发布于 2024-10-14 08:55:41 字数 105 浏览 6 评论 0原文

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

当我单击提交按钮时,这些值应该出现在另一个页面中。任何人都可以举个例子吗?我完全困惑了。

请帮我 谢谢

Suppose i have two text fields in one page, one for name and another one for age.

When i click the submit button those values should appear in another page. Can any one give the example for that one.. i am totally confused.

please help me
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-10-21 08:55:41

MSDN 有一个关于此的页面,如何:在 ASP.NET 网页之间传递值< /a>:

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

  • 使用查询字符串。
  • 从以下位置获取 HTTP POST 信息
    源页面。

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

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

对于您的场景,听起来使用 POST 是可行的方法,因为您的第一页上有文本框。示例:

第一页:

<%@ 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" 将 POST 定向到第二页。没有隐藏代码。

Page 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>

请注意 Page 元素上的 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

应该可以...:)

MSDN has a page on this, How to: Pass Values Between ASP.NET Web Pages:

The following options are available even if the source page is in a different ASP.NET Web application from the target page, or if the source page is not an ASP.NET Web page:

  • Use a query string.
  • Get HTTP POST information from the
    source page.

The following options are available only when the source and target pages are in the same ASP.NET Web
application.

  • Use session state.
  • Create public properties in the source page and access the property values in the target page.
  • Get control information in the target page from controls in the source page.

For your scenario, it sounds like using POST is the way to go, since you have textboxes on the first page. Example:

First page:

<%@ 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>

Notice the action="WebForm2.aspx" which directs the POST to the second page. There's no code-behind.

Page 2 (receiving page):

<%@ 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>

Notice the EnableViewStateMac="false" attribute on the Page element. It's important.

The code-behind, grabbing the values using a simple 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

That should work... :)

那些过往 2024-10-21 08:55:41

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

private void btnSubmit_Click(对象
发送者,System.EventArgs e) {
Response.Redirect("AnotherPage.aspx?Name="
+ this.txtName.Text + "&Age=" + this.txtAge.Text); }

代码放入第二页page_load,

private void Page_Load(对象发送者,
System.EventArgs e) {
this.txtBox1.Text =
Request.QueryString["名称"];
this.txtBox2.Text =
Request.QueryString["年龄"]; }

Put this code to your submit button event handler,

private void btnSubmit_Click(object
sender, System.EventArgs e) {
Response.Redirect("AnotherPage.aspx?Name="
+ this.txtName.Text + "&Age=" + this.txtAge.Text); }

Put this code to second page page_load,

private void Page_Load(object sender,
System.EventArgs e) {
this.txtBox1.Text =
Request.QueryString["Name"];
this.txtBox2.Text =
Request.QueryString["Age"]; }

护你周全 2024-10-21 08:55:41

你有几个选择。
**

<强>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

You have a couple of options.
**

1. Use Query string.


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

2. Use Session

(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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文