473,387 Members | 1,535 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,387 software developers and data experts.

Scope and values of Object

I'm trying to set a level of abstraction in an ASP.NET app. I've created an
object with properties and methods that I instansiate just after the class
declaration. As I'm debugging this code I find that on postback the object is
re-instanciated, wiping all the property values. A simplified version of what
I'm trying to do is as follows:

Using System;
Using ...

namespace Test
public class Test
{
Obj o = new Obj;

private void Page_Load(object sneder, System.EventArgs e)
{
if (! IsPostBack)
{
o.property1 = true;
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.submit.Click += System.EventHandler(this.submit_click);
}
private void submit_click()
{
Response.Write("<script language='javascript'> {alert("Value
of o.Property1 = " + o.Property1);}</script>");
}
}

After postback, the object is instanciated (again) and all the properties
are reset. How do I keep the scope (and values) throught the life of the form?

Thanks!
Mar 17 '06 #1
1 1331
You will have to use a ViewState to persist your object, and also ensure that
object can be serialized. Make following changes to your Page_Load

Obj o = null;
....
private void Page_Load(object sneder, System.EventArgs e)
{
if (! IsPostBack)
{
o = new Obj();
o.property1 = true;
//save it in page's view state
ViewState["myObject"] = o;
}

o = ViewState["myObject"] as Obj;
}

After this your submit click should work just fine, and object will get
instantiated only when page is not posted back, and then saved into the view
state.
"AbeR" wrote:
I'm trying to set a level of abstraction in an ASP.NET app. I've created an
object with properties and methods that I instansiate just after the class
declaration. As I'm debugging this code I find that on postback the object is
re-instanciated, wiping all the property values. A simplified version of what
I'm trying to do is as follows:

Using System;
Using ...

namespace Test
public class Test
{
Obj o = new Obj;

private void Page_Load(object sneder, System.EventArgs e)
{
if (! IsPostBack)
{
o.property1 = true;
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.submit.Click += System.EventHandler(this.submit_click);
}
private void submit_click()
{
Response.Write("<script language='javascript'> {alert("Value
of o.Property1 = " + o.Property1);}</script>");
}
}

After postback, the object is instanciated (again) and all the properties
are reset. How do I keep the scope (and values) throught the life of the form?

Thanks!

Mar 18 '06 #2

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

Similar topics

3
by: Simon Niederberger | last post by:
How can I give a user-defined object session scope? From http://www.microsoft.com/windows2000/en/server/iis/default.asp?url=/windows2000/en/server/iis/htm/asp/iiwaobu.htm I gather that it can...
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
12
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
2
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.