假設我在一個頁面中有兩個文本字段,一個用於名稱和另一個用於年齡。如何將值從一個頁面到另一個頁面在asp.net使用vb
當我點擊提交按鈕時,這些值應該出現在另一個頁面。任何一個人都可以給出這個例子..我完全困惑。
請幫我 謝謝
假設我在一個頁面中有兩個文本字段,一個用於名稱和另一個用於年齡。如何將值從一個頁面到另一個頁面在asp.net使用vb
當我點擊提交按鈕時,這些值應該出現在另一個頁面。任何一個人都可以給出這個例子..我完全困惑。
請幫我 謝謝
MSDN對這個頁面,How to: Pass Values Between ASP.NET Web Pages:
下列選項可用,即使源頁在來自目標頁面的不同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>
通知其引導POST到第二頁的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
這應該工作... :)
將這個代碼添加到您提交按鈕的事件處理程序,
私人無效btnSubmit_Click(對象 發件人,發送System.EventArgs){ 的Response.Redirect(」 AnotherPage.aspx?Name =「 + this.txtName.Text +」& Age =「+ this.txtAge.Text); }
將這個代碼到第二頁面的Page_Load,
私人無效的Page_Load(對象發件人, System.EventArgs發送){ this.txtBox1.Text = 的Request.QueryString [ 「名稱」 ]。 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.使用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
@ Nanda..here txtAge和txtName的是什麼?文本框名稱或ID?當我使用這個屬性時,它顯示錯誤。最後它不起作用。 – Mihir 2011-01-27 05:18:07