473,383 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

c# page level global object postback

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
Nov 18 '05 #1
4 1871
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

Nov 18 '05 #2
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)
"Cowboy (Gregory A. Beamer) - MVP" <No************@comcast.netNoSpamM> wrote
in message news:89**********************************@microsof t.com...
| 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
Nov 18 '05 #3
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
"Marty U." <Ma****@discussions.microsoft.com> wrote in message
news:AA**********************************@microsof t.com...
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

Nov 18 '05 #4
You can decleare that variable as static, e.g.

static MyObject m;

--
Vanni Tomezzoli
S. Donato Mil., Milano, Italy
http://xoomer.virgilio.it/vtomezzo
"Marty U." <Ma****@discussions.microsoft.com> ha scritto nel messaggio
news:AA**********************************@microsof t.com...
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

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: moondaddy | last post by:
I'm building a page in vb.net with several user controls on it. I'm using user controls instead of a frames page since I've seen this recommended many times in this user group. I want just one of...
5
by: Fred Nelson | last post by:
Hi: I'm a relative newby so hopefully this is a simple question! I have found that I can create global variables easily on a web page by placing the dim statement before the first "private...
2
by: John Lau | last post by:
Hi, Is there documentation that talks about the page lifecycle, the lifecycle of controls on the page, and the rendering of inline code, in a single document? Thanks, John
8
by: hb | last post by:
Hi, I need to declare a variable who's value can be preserve through the same ASP.Net page. I tried the following code, only the static variable s2 keeps its value=22 after lnk1_Click followed...
7
by: UJ | last post by:
I've got a page with a user control on it. While the page is loading, it needs to check certain conditions of the user object to enable/disable things on the screen. Currently in the page_load of...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
9
by: Jason | last post by:
I have translated my website into Japanese, so if someone visits this page in English: http://dheera.net/jason/about/interests/music/index.html I want them to be able to click on word...
9
by: ad | last post by:
Hi, How can I dynamically change the MasterPager of a web page?
4
by: Blasting Cap | last post by:
I have a page that has a number of radio buttons that will be displayed to different access levels of a user who logs in to my website. For instance, if there are a dozen buttons, user1 will see...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.