Gabbyboy
Gabbyboy

Reputation: 1524

Reference of an object to a Session property

I have a text box in my aspx page, I created a Session property (textbox type), then:

textBoxInSession = myTextBox;

If I change the Text property in textBoxInSession, the Text property in myTextBox does not changed.

isn't textBoxInSession a reference to myTextBox?

Upvotes: 0

Views: 139

Answers (4)

wonkim00
wonkim00

Reputation: 657

In general, you should not persist UI elements/controls to the Session or Application state. When you store an object instance, you pin it and everything that holds a reference to it in memory. Because ASP.NET creates a new instance on each page execution, this can result in considerable amounts of memory being consumed by non-garbage-collectible Page and Control instances, if you make heavy use of this technique.

You should store value types, strings and serializable POCOs or business layer types instead, and rebind them to UI controls when you need them.

Upvotes: 2

Charles Lambert
Charles Lambert

Reputation: 5132

Each request for a page is handled by a different thread, and a new instance of your class is created for that request. The Textbox stored in Session is the TextBox from the previous instance of your class from the previous page request.

The behavior you expect will work if it is during the same request. However that really serves no purpose since you can reference the TextBox directly.

Upvotes: 0

Hans Kesting
Hans Kesting

Reputation: 39338

When your page serves a new Request, it's a new instance of the page, containing new instances of your controls. So that TextBox is another instance than what you stored in Session on a previous Request.

Upvotes: 1

Schroedingers Cat
Schroedingers Cat

Reputation: 3139

The testBixInSession is a copy of your page text box, not a reference.

Upvotes: 0

Related Questions