I want to have page level access to a certain object but cant figure out how
to do this. I create a class method that returns the object, but I cant get
it to be global to the page. By global to the page, I mean to let all
methods and event handlers have access to the same instance of the object. I
looked at a global.asax file, but that doesn't seem to be what I am looking
for (since the object is supposed to be page wide not application wide). Any
ideas how I can do this?
Juan T. Llibre - 29 Apr 2008 08:11 GMT
Place your object's class in the App_Code folder,
or compile an assembly from the source class
and place it in the /bin directory of your application.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
>I want to have page level access to a certain object but cant figure out how to do this. I create a class method that
>returns the object, but I cant get it to be global to the page. By global to the page, I mean to let all methods and
>event handlers have access to the same instance of the object. I looked at a global.asax file, but that doesn't seem to
>be what I am looking for (since the object is supposed to be page wide not application wide). Any ideas how I can do
>this?
Eliyahu Goldin - 29 Apr 2008 10:59 GMT
Perhaps I am missing something. Why don't you just declare a reference to
the oblect:
private MyClass myObject;
in the page class?

Signature
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
>I want to have page level access to a certain object but cant figure out
>how to do this. I create a class method that returns the object, but I cant
[quoted text clipped - 3 lines]
>looking for (since the object is supposed to be page wide not application
>wide). Any ideas how I can do this?
Andy B - 29 Apr 2008 14:53 GMT
I have this code:
//this code works in the page class
//Create a ContractManager so we can have an object that will create, save,
convert the object and other things as well. This I would like to be web
application
//global. I.e. once the instance is created for the whole application, it
doesn't need to be recreated.
ContractManager Manager = new ContractManager();
//Once the ContractManager is created, I now need to create the object
itself. This requires the following code which wont work inside a page class
since it is
//executable code. This is where I run into huge problems since the
following object needs to be accessible by all of the methods/event handlers
of the class during
//its entire lifecycle.
//This creates the object, assigns default values and returns a Contract
object and assigns it to StockContract.
Contract StockContract = Manager.CreateEmptyStockContract();
Hope this helps with what I need.
> Perhaps I am missing something. Why don't you just declare a reference to
> the oblect:
[quoted text clipped - 10 lines]
>>am looking for (since the object is supposed to be page wide not
>>application wide). Any ideas how I can do this?
bruce barker - 29 Apr 2008 17:10 GMT
as long as you realize the page lifecycle is for only one request, as a new
page object is created per request, just create a class variable:
private Contract StockContract = Manager.CreateEmptyStockContract();
this will be available to all methods in the page class. if you need the
same instance on a postback, then you will need to come up up a way to
save/restore the object between postbacks. the session object can help here.
-- bruce (sqlwork.com)
> I have this code:
> //this code works in the page class
[quoted text clipped - 32 lines]
> >>am looking for (since the object is supposed to be page wide not
> >>application wide). Any ideas how I can do this?
Andy B - 29 Apr 2008 17:50 GMT
Contract StockContract = Manager.CreateEmptyStockContract();
Doesn't actually work in just a class. I get a compiler error saying that
this statement is invalid unless inside a method of some kind. I will need
the StockContract object available to all methods/event handlers and it will
have to be around during postbacks.
> as long as you realize the page lifecycle is for only one request, as a
> new
[quoted text clipped - 52 lines]
>> >>am looking for (since the object is supposed to be page wide not
>> >>application wide). Any ideas how I can do this?