I am trying to make a strongly typed viewstate object.
public class MyNewPage : ViewStatePage<MyNewPageViewStateStore>
In the implementation of ViewStatePage, I am instantiating an object Store
out of this.ViewState["_PageViewState"]
. In the derived page, I'll be using that object and making changes to it.
What's the last event called in an asp.net page that can still affect View State?
Is it Unload
?
In the ASP.net page lifecycle the order goes (this is the short version):
Preinit -> Init -> Load Viewstate -> Page Load -> Events -> PreRender -> Save Viewstate -> Render -> Unload
Places where changes to viewstate are persisted in Bold. As a result the last chance you have to modify viewstate and have it persisted is PreRender (technically you could handle SaveViewState and that would be your last chance to modify viewstate and have it saved).
MSDN: http://msdn.microsoft.com/en-us/library/ms178472.aspx#additional_page_life_cycle_considerations
Handling SaveViewState:
public partial class MyPage: System.Web.UI.Page
{
protected override object SaveViewState()
{
// manipulate viewstate objects here -- Last Chance
return base.SaveViewState();
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
来源:https://stackoverflow.com/questions/8265831/last-event-in-page-that-can-still-affect-a-pages-viewstate