Variables?!?

  • Thread starter Thread starter ChrisN
  • Start date Start date
C

ChrisN

Hi All,

I don't understand why this is happing. If anyone knows this please fill me
in. I've simplified my question for the sake of simplicity that's why my
example sounds cheezy.

1. I have a VB.Net page with the following items:

-Textbox for Author
-Button for Okay
-A few other objects that cause postback events (eg DropDownList).

You enter an author's name into the textbox and press the okay button.

2. The page calls a C# object that takes the name and looks up the author in
a database. This information is returned as an object that is stored
locally on the page. It then lists the author's information from this
object in a table on the page. This part works fine.

3. Whenever any of the other items on the page cause a postback the returned
object becomes "Nothing" and the author's information is no longer
displayed.

How can I keep the object from being cleared during a postback event by the
other objects? The object is stored globally on the page so it should not
be destroyed. I store integer values on the page the same way and they
don't get cleared during a postback. What am I doing wrong?

Thanks,
Chris.
 
ChrisN said:
Hi All,

I don't understand why this is happing. If anyone knows this please fill me
in. I've simplified my question for the sake of simplicity that's why my
example sounds cheezy.

1. I have a VB.Net page with the following items:

-Textbox for Author
-Button for Okay
-A few other objects that cause postback events (eg DropDownList).

You enter an author's name into the textbox and press the okay button.

2. The page calls a C# object that takes the name and looks up the author in
a database. This information is returned as an object that is stored
locally on the page. It then lists the author's information from this
object in a table on the page. This part works fine.

3. Whenever any of the other items on the page cause a postback the returned
object becomes "Nothing" and the author's information is no longer
displayed.

How can I keep the object from being cleared during a postback event by the
other objects? The object is stored globally on the page so it should not
be destroyed. I store integer values on the page the same way and they
don't get cleared during a postback. What am I doing wrong?

The Page should be assumed to be always destroyed after each request.
Your C# object can persist only if it's saved into viewstate (as your
integer values), Session, Cache, or AppState.

The default value of EnableViewState is true, so that all properties of
your Page are saved into the viewstate automatically. But your object
needs to be serializable, or have a TypeConverter defined, see
http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp

You may also use Cache instead of ViewState.
 
Thanks! I think I figured it out now.

Aquila Deus said:
The Page should be assumed to be always destroyed after each request.
Your C# object can persist only if it's saved into viewstate (as your
integer values), Session, Cache, or AppState.

The default value of EnableViewState is true, so that all properties of
your Page are saved into the viewstate automatically. But your object
needs to be serializable, or have a TypeConverter defined, see
http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp

You may also use Cache instead of ViewState.
 
Does that mean if Page.EnableViewState is true (by default), then the Page
object is *not* destroyed for each request?

Thanks,
Simon
 
Simon said:
Does that mean if Page.EnableViewState is true (by default), then the Page
object is *not* destroyed for each request?

It doesn't matter. The page is still destroyed, but all of its
properties are automatically saved into the viewstate.
 
Back
Top