c# page level global object postback

G

Guest

How can you declare an object that is global to a page even on postbacks?

I currently have something like this

MyObject m;

throughout the page on the initial load everything works fine but on
postback anything referencing m I get NullReference.

How can I declare an object to have page level scope through postback.

Thanks
 
G

Guest

The easiest is to stick the value into ViewState:

ViewState["ValueIWishToKeep"] = value;

You can then pull it back out of ViewState, remembering to cast it out as
the type you wish it to be:

value = ViewState["ValueIWishToKeep"].ToString();

for example.

Remember that web apps are stateless. The other option is session, but this
makes it "global" to all pages.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
B

bruce barker

unless your app is on the local lan, you should avoid using ViewState and
increasing the payload. you should implement some sort of server based
session support. you can store the session key in viewstate.

-- bruce (sqlwork.com)


in message | The easiest is to stick the value into ViewState:
|
| ViewState["ValueIWishToKeep"] = value;
|
| You can then pull it back out of ViewState, remembering to cast it out as
| the type you wish it to be:
|
| value = ViewState["ValueIWishToKeep"].ToString();
|
| for example.
|
| Remember that web apps are stateless. The other option is session, but
this
| makes it "global" to all pages.
|
|
| ---
|
| Gregory A. Beamer
| MVP; MCP: +I, SE, SD, DBA
|
| ***************************
| Think Outside the Box!
| ***************************
|
| "Marty U." wrote:
|
| > How can you declare an object that is global to a page even on
postbacks?
| >
| > I currently have something like this
| >
| > MyObject m;
| >
| > throughout the page on the initial load everything works fine but on
| > postback anything referencing m I get NullReference.
| >
| > How can I declare an object to have page level scope through postback.
| >
| > Thanks
 
K

Kikoz

Hi.

If it's a light weight object and don't do much at initialization you can
declare it as a global variable in your Page's class and instantiate every
time page loads. But that would highly depend on particular situation.

Hope it helps,
Kikoz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top