1. 使用QueryString變數
QueryString是一種非常簡單的傳值方式,他可以將傳送的值顯示在瀏覽器的位址列中。如果是傳遞一個或多個安全性要求不高或是結構簡單的數值時,可以使用這個方法。但是對於傳遞陣列或物件的話,就不能用這個方法了。下面是一個例子:
a.aspx的VB.NET程式碼
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim s_url As String
s_url = "b.aspx?name=" + Label1.Text
Response.Redirect(s_url)
End Sub
Dim s_url As String
s_url = "b.aspx?name=" + Label1.Text
Response.Redirect(s_url)
End Sub
b.aspx中VB.NET程式碼
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Label2.Text = Request.QueryString("name")
End Sub
Label2.Text = Request.QueryString("name")
End Sub
2. 使用Application 物件變數
Application物件的作用範圍是整個全域性,也就是說對所有使用者都有效。其常用的方法用Lock和UnLock。
a.aspx的VB.NET程式碼
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Application("name") = Label1.Text
Server.Transfer("b.aspx")
End Sub
Application("name") = Label1.Text
Server.Transfer("b.aspx")
End Sub
b.aspx中VB.NET程式碼
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String
Application.Lock()
name = Application("name").ToString()
Application.UnLock()
End Sub
Dim name As String
Application.Lock()
name = Application("name").ToString()
Application.UnLock()
End Sub
3. 使用Session變數
想必這個肯定是大家使用中最常見的用法了,其操作與Application類似,作用於使用者個人,所以,過量的儲存會導致伺服器記憶體資源的耗盡。
a.aspx的VB.NET程式碼
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Session("name") = Label.Text
End Sub
Session("name") = Label.Text
End Sub
b.aspx中VB.NET程式碼
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String
name = Session("name").ToString()
End Sub
Dim name As String
name = Session("name").ToString()
End Sub
4. 使用Cookie物件變數
這個也是大家常使用的方法,與Session一樣,其是什對每一個使用者而言的,但是有個本質的區別,即Cookie是存放在客戶端的,而session是存放在伺服器端的。而且Cookie的使用要配合ASP.NET內建物件Request來使用。
a.aspx的VB.NET程式碼
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cookie_name As HttpCookie = New HttpCookie("name")
cookie_name.Value = Label1.Text
Reponse.AppendCookie(cookie_name)
Server.Transfer("b.aspx")
End Sub
Dim cookie_name As HttpCookie = New HttpCookie("name")
cookie_name.Value = Label1.Text
Reponse.AppendCookie(cookie_name)
Server.Transfer("b.aspx")
End Sub
b.aspx中VB.NET程式碼
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String
name = Request.Cookie("name").Value.ToString()
End Sub
Dim name As String
name = Request.Cookie("name").Value.ToString()
End Sub
5. 使用Server.Transfer方法
這個才可以說是面象物件開發所使用的方法,其使用Server.Transfer方法把流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以這個方法是完全面象物件的,簡潔有效。
a.aspx的VB.NET程式碼
Public ReadOnly Property Name() As String
Get
Return Label1.Text
End Get
End Property
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Server.Transfer("b.aspx")
End Sub
Get
Return Label1.Text
End Get
End Property
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Server.Transfer("b.aspx")
End Sub
b.aspx中VB.NET程式碼
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim NewWeb As a '例項a窗體
NewWeb = CType(Context.Handler, source)
Dim name As String
name = NewWeb.Name
End Sub
Dim NewWeb As a '例項a窗體
NewWeb = CType(Context.Handler, source)
Dim name As String
name = NewWeb.Name
End Sub