ViewState issue.

W

Wei

Hi,
I have a simple custom button control which inherits from
the button web control. All it does is to display the
number of clicks. I am using the ViewState to record the
count, and I initially thought that the construstor will
only be executed once, so I initialized the ViewState
["Count"]=0 in it. The program runs fine. But when I debug
into the code, the thing suprises me is the constructor
gets called in each postback, which means the ViewState
["Count"] gets reset to 0 during each postback. But when
the value is fetched in the OnClick event, seems it gets
the value before it is reset which I cannot understand.
See code below.

I will appreciate anyone can figure out why.

Thanks,

Wei


public class CountedButton :
System.Web.UI.WebControls.Button
{
public CountedButton()
{// constructor is called in each postback
this.Text = "Click me";
ViewState["Count"] = 0;// it's reset to 0 each time
}

protected override void OnClick(EventArgs e)
{ // but why after the reset in constructor, the
View state keeps the original value(such as 2, 3,4) here???
ViewState["Count"]=((int)ViewState["Count"]) + 1;
this.Text = ViewState["Count"] + " clicks";
base.OnClick(e);
}

// count as property maintained in view state
public int Count
{
get{return (int) ViewState["Count"];}
set{ViewState["Count"] = value;}
}
}
 
T

Teemu Keiski

Page and it's controls are recreated on every request therefore constructors
will also be called on every request. Another thing is that ViewState is
loaded after constructor on every request as lifecycle is:

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose


Wouldn't it be OK without the resetting as you now have the Count property
that uses ViewState? ViewState is valid only on initial request and
subsequent postbacks by the same user, so it would be automatically zero
for any other user.

--
Teemu Keiski
MCP,Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com
 
W

Wei

Thank you Teemu.

To my case, does that mean that the view state values set
in the constructor in step 2(Initialize) is overwriten by
the one(if available) in step 4(LoadViewState)?
-----Original Message-----
Page and it's controls are recreated on every request therefore constructors
will also be called on every request. Another thing is that ViewState is
loaded after constructor on every request as lifecycle is:

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose


Wouldn't it be OK without the resetting as you now have the Count property
that uses ViewState? ViewState is valid only on initial request and
subsequent postbacks by the same user, so it would be automatically zero
for any other user.

--
Teemu Keiski
MCP,Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com




Wei said:
Hi,
I have a simple custom button control which inherits from
the button web control. All it does is to display the
number of clicks. I am using the ViewState to record the
count, and I initially thought that the construstor will
only be executed once, so I initialized the ViewState
["Count"]=0 in it. The program runs fine. But when I debug
into the code, the thing suprises me is the constructor
gets called in each postback, which means the ViewState
["Count"] gets reset to 0 during each postback. But when
the value is fetched in the OnClick event, seems it gets
the value before it is reset which I cannot understand.
See code below.

I will appreciate anyone can figure out why.

Thanks,

Wei


public class CountedButton :
System.Web.UI.WebControls.Button
{
public CountedButton()
{// constructor is called in each postback
this.Text = "Click me";
ViewState["Count"] = 0;// it's reset to 0 each time
}

protected override void OnClick(EventArgs e)
{ // but why after the reset in constructor, the
View state keeps the original value(such as 2, 3,4) here???
ViewState["Count"]=((int)ViewState["Count"]) + 1;
this.Text = ViewState["Count"] + " clicks";
base.OnClick(e);
}

// count as property maintained in view state
public int Count
{
get{return (int) ViewState["Count"];}
set{ViewState["Count"] = value;}
}
}


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,076
Messages
2,570,565
Members
47,201
Latest member
IvyTeeter

Latest Threads

Top