Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StateBag object question-can you help?

Status
Not open for further replies.

Visiting

IS-IT--Management
Nov 26, 2002
21
US
This is probably a little weird question.
In this example everytime button is clicked,counter is incremented

<%@ Page Language=&quot;C#&quot; %>
<html>

<script runat=&quot;server&quot;>
void Page_Load(Object sender, EventArgs e)
{
lblCounter.Text = Counter.ToString();
Counter++;
}

public int Counter
{
get
{ //is an element with &quot;intX&quot; key
from page's StateBack object?
if (ViewState[&quot;intX&quot;] != null )
{
return ((int)ViewState[&quot;intX&quot;]);
}
return 0;
}

set
{
ViewState[&quot;intX&quot;] = value;
}
}
</script>
<body>

<form runat=&quot;server&quot; ID=&quot;Form

<asp:Label id=&quot;lblCounter&quot; runat=&quot;server&quot; />
<asp:Button id=&quot;btn&quot; text=&quot;Increment Counter&quot; runat=&quot;server&quot; />
</form>

</body>
</html>

Can someone tell me,why in this example there isn't any StateBag object defined?
Is it because ASP.NET automaticly creates one for your page?
DOES an element with intX key exist in page's StateBag object?
And last,have controls(for instance some Label control) their own StateBag objects created or do they all store their view state in the same StateBag instance?

Thank you for your help
 
I've got a question: Whats a &quot;StateBag&quot;?!

It sounds kinda....dirty.

Anyway, here's whats happening in the code:

When the counter is being gotten or set, its actually getting/setting the value to a Viewstate variable.

The Viewstate is kinda like your notion of a &quot;StateBag&quot;. It holds values that the page uses in a hidden field, where the values are hashed (its the big long &quot;wtf is all that&quot; string when you view source of an asp.net page).

So although there's no object being created per-se, the Viewstate object is being used to keep track of the counter.
:)

Jack
 
&quot;ViewState&quot; is the name of the property in the Page object that holds a reference to a StateBag object, i.e. Page.ViewState. There is no &quot;ViewState&quot; object. It is a &quot;made up&quot; property name.

The StateBag object for the Page object is &quot;behind&quot; the scenes in the Page object.
e.g.
Private mobjViewState As StateBag Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Interesting. My thoughts:

Even though the Viewstate is definately an interface to the StateBag, it would seem that developers don't really need to know that the Statebag exists, and that accessing the Viewstate is the intended method of storing items accross postbacks.

In the CodeBehind for an aspx page, there's never a need to access the Viewstate via Page.Viewstate, you can directly go to the Viewstate &quot;object&quot; itself (and the interface really is meant to give that feeling of dealing with an object, regardless of whether its really just a property. Many articles refer to Viewstate as an object as well).

Also, although you could definately define a StateBag object, I fail to see a situation where you'd need to.

Regardless, just splitting the &quot;Viewstate is/is not an object&quot; hair. Thanks for the post; I had no idea the StateBag even existed.
:)

Jack
 
Thank you.If I'm not mistaken you'r telling me there is only one StateBag object for all controls in a Page object?
 
There is a Viewstate property for every control on the page including the page itself which is a control. However, the ViewState property is a protected property of the underlying control class but is overridden for the Page class. It is possible that all the ViewState properties point to the same StateBag object but it is impossible to test without using the System.Reflection class to access the protected property of one of the controls and I do not have time right now to check it. Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top