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