加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net在页面之间传递类的实例

发布时间:2020-12-16 09:23:25 所属栏目:asp.Net 来源:网络整理
导读:在第一页,我想创建一个类的实例: pOne = New pClass() xOne = New xClass(pOne) 然后在后续页面中我希望能够使用pOne和xOne.由于pOne和xOne是第一页的本地,我如何在其他页面中使用它们? 解决方法 您可以使用Session变量来存储该对象并在另一个页面中使用它
在第一页,我想创建一个类的实例:

pOne = New pClass()
    xOne = New xClass(pOne)

然后在后续页面中我希望能够使用pOne和xOne.由于pOne和xOne是第一页的本地,我如何在其他页面中使用它们?

解决方法

您可以使用Session变量来存储该对象并在另一个页面中使用它.

//Set the session
Session["p1"]=pOne;
Session["x1"]=xOne;

在第二页,阅读会话

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 
}

在访问变量之前始终进行空检查是一种很好的做法

这是VB.NET版本(我希望这个有用,我在VB.NET上没有太多经验)

// Set the Session
 Session("p1")=pOne
 Sesssion("x1")=xOne

在第二页阅读会话,

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读