English 中文(简体)
页: 1
原标题:asp.net passing an instance of a class between pages

On page one I would like to create an instance of a class as such:

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

Then on subsequent pages I would like to be able to use pOne and xOne. Since pOne and xOne are local to page one how can I use them in other pages?

最佳回答

You may use Session variable to store that object and use it in another page.

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

页: 1

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 
}

在获得可变变量之前总是进行无效检查是一种良好做法。

www.un.org/Depts/DGACM/index_spanish.htm 这里是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
问题回答

页: 1 HttpContext. 项目,然后,您不必清理ling物,除非再造:

rel=“nofollow”> 利用HttpContext 项目藏书通过横跨的物体





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签