473,748 Members | 7,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1882
The easiest is to stick the value into ViewState:

ViewState["ValueIWishToKe ep"] = 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["ValueIWishToKe ep"].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.netNoS pamM> wrote
in message news:89******** *************** ***********@mic rosoft.com...
| The easiest is to stick the value into ViewState:
|
| ViewState["ValueIWishToKe ep"] = 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["ValueIWishToKe ep"].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****@discuss ions.microsoft. com> wrote in message
news:AA******** *************** ***********@mic rosoft.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****@discuss ions.microsoft. com> ha scritto nel messaggio
news:AA******** *************** ***********@mic rosoft.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
5730
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 the user controls to do a postback but not the rest of the page. Is this possible? it seems like it should be. If it is, please explain how I can do it. Thanks. -- moondaddy@nospam.com
5
9260
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 sub" in a program: dim mydata as (whatever) Public Sub Page_Load(etc..)
2
3626
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
9204
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 by lnk2_Click(). The others return to their original values initiated during their declaration. But the static variable's life doesn't end when the page is closed and it causes
7
23351
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 the user control I do things like read from the database on the first load, set some internal variables, ... If I need those things to be available to me on the page load, should I do them on page_init? Are there any ramifications of using...
15
4777
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 button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
9
1673
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 日本語 at the bottom to go to the corresponding page in Japanese, which would be: http://dheera.net/jason/ja/about/interests/music/index.html
9
16272
by: ad | last post by:
Hi, How can I dynamically change the MasterPager of a web page?
4
2519
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 all twelve. User2 would see buttons 1-3,10-12 and NOT any others, and so on. Since there are several buttons on the page (each one would run a report for them), is there a way to - in an onclick event for each of the buttons - to...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.