在第一页,我想创建一个类的实例:
pOne = New pClass()
xOne = New xClass(pOne)
Run Code Online (Sandbox Code Playgroud)
然后在后续页面中我希望能够使用pOne和xOne.由于pOne和xOne是第一页的本地,我如何在其他页面中使用它们?
您可以使用Session变量来存储该对象并在另一个页面中使用它.
//Set the session
Session["p1"]=pOne;
Session["x1"]=xOne;
Run Code Online (Sandbox Code Playgroud)
在第二页,阅读会话
if(Session["p1"]!=null)
{
// If object is present in session, Cast that to our class (PClass) type
PClass objP1=(PClass) Session["p1"];
//Now you can use objP1
}
if(Session["x1"]!=null)
{
XClass objx1=(XClass) Session["x1"];
//Now you can use objx1
}
Run Code Online (Sandbox Code Playgroud)
在访问变量之前始终进行空检查是一种很好的做法
这是VB.NET版本(我希望这个有用,我在VB.NET上没有太多经验)
// Set the Session
Session("p1")=pOne
Sesssion("x1")=xOne
Run Code Online (Sandbox Code Playgroud)
在第二页阅读会话,
if Session("p1") IsNot Nothing Then
Dim objP1 As pClass
objP1=CType(Session("p1"),pClass)
'Now you can use objP1
End If
if Session("x1") IsNot Nothing Then
Dim objX1 As xClass
objX1=CType(Session("x1"),xClass)
'Now you can use objX1
End If
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2680 次 |
最近记录: |