Why is SaveViewState called twice in the Control Life Cycle?

Discussion in 'ASP .Net Building Controls' started by Stephen Miller, Dec 31, 2003.

  1. Hi,

    I'm just looking at the web control life cycle and it appears that the
    SaveViewState method SaveViewState is called twice.

    To demonstrate, I overrode each method of the Web Control Life Cycle
    with a 'Page.Trace' command (note that the string '-> SaveViewState'
    appears only once):

    // 2: Object Initialization
    protected override void OnInit(EventArgs e) {
    Page.Trace.Write (" -> OnInit");
    base.OnInit(e);
    if (Page != null){
    Page.RegisterRequiresPostBack(this);
    }
    }

    // 3: Begin Tracking View State
    protected override void TrackViewState () {
    Page.Trace.Write (" -> TrackViewState");
    base.TrackViewState();
    }

    // 3a: Postback -> LoadViewState
    protected override void LoadViewState(Object viewState) {
    Page.Trace.Write (" -> LoadViewState");
    if (viewState == null) {
    base.LoadViewState(viewState);
    }
    }

    // 4: Load
    protected override void OnLoad (EventArgs e) {
    Page.Trace.Write (" -> OnLoad");
    base.OnLoad(e);
    }

    // 5: PreRender
    protected override void OnPreRender (EventArgs e) {
    Page.Trace.Write (" -> OnPreRender");
    base.OnPreRender(e);
    }

    // 6: Save View State
    protected override object SaveViewState () {
    Page.Trace.Write (" -> SaveViewState");
    return base.SaveViewState();
    }

    // 7: Render
    protected override void RenderContents(HtmlTextWriter writer){
    Page.Trace.Write (" -> RenderContents");
    writer.Write ("my test control");
    }

    // 9: Dispose
    public override void Dispose(){
    Page.Trace.Write (" -> Dispose");
    base.Dispose();
    }

    I then compiled my control and added to a new WebForm and enabled
    tracing in the page directive ('<%@ Page Trace=true ...'). The Trace
    Information for the page indicates that my overridden SaveViewState
    method was called twice, immediately before and after the 'Begin
    SaveViewState' section:

    Begin Init
    -> OnInit
    -> TrackViewState
    End Init
    -> OnLoad
    Begin PreRender
    -> OnPreRender
    End PreRender
    -> SaveViewState
    Begin SaveViewState
    -> SaveViewState
    End SaveViewState
    Begin Render
    -> RenderContents
    End Render

    Is this expected? If so what does it mean?

    Thanks,

    Stephen
    Stephen Miller, Dec 31, 2003
    #1
    1. Advertising

  2. Stephen Miller

    Teemu Keiski Guest

    Hi,

    you are using tracing when you see this, right? The duplicate call occurs
    only when you use tracing because page makes additional call to estimate the
    size of view state.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "Stephen Miller" <> wrote in message
    news:...
    > Hi,
    >
    > I'm just looking at the web control life cycle and it appears that the
    > SaveViewState method SaveViewState is called twice.
    >
    > To demonstrate, I overrode each method of the Web Control Life Cycle
    > with a 'Page.Trace' command (note that the string '-> SaveViewState'
    > appears only once):
    >
    > // 2: Object Initialization
    > protected override void OnInit(EventArgs e) {
    > Page.Trace.Write (" -> OnInit");
    > base.OnInit(e);
    > if (Page != null){
    > Page.RegisterRequiresPostBack(this);
    > }
    > }
    >
    > // 3: Begin Tracking View State
    > protected override void TrackViewState () {
    > Page.Trace.Write (" -> TrackViewState");
    > base.TrackViewState();
    > }
    >
    > // 3a: Postback -> LoadViewState
    > protected override void LoadViewState(Object viewState) {
    > Page.Trace.Write (" -> LoadViewState");
    > if (viewState == null) {
    > base.LoadViewState(viewState);
    > }
    > }
    >
    > // 4: Load
    > protected override void OnLoad (EventArgs e) {
    > Page.Trace.Write (" -> OnLoad");
    > base.OnLoad(e);
    > }
    >
    > // 5: PreRender
    > protected override void OnPreRender (EventArgs e) {
    > Page.Trace.Write (" -> OnPreRender");
    > base.OnPreRender(e);
    > }
    >
    > // 6: Save View State
    > protected override object SaveViewState () {
    > Page.Trace.Write (" -> SaveViewState");
    > return base.SaveViewState();
    > }
    >
    > // 7: Render
    > protected override void RenderContents(HtmlTextWriter writer){
    > Page.Trace.Write (" -> RenderContents");
    > writer.Write ("my test control");
    > }
    >
    > // 9: Dispose
    > public override void Dispose(){
    > Page.Trace.Write (" -> Dispose");
    > base.Dispose();
    > }
    >
    > I then compiled my control and added to a new WebForm and enabled
    > tracing in the page directive ('<%@ Page Trace=true ...'). The Trace
    > Information for the page indicates that my overridden SaveViewState
    > method was called twice, immediately before and after the 'Begin
    > SaveViewState' section:
    >
    > Begin Init
    > -> OnInit
    > -> TrackViewState
    > End Init
    > -> OnLoad
    > Begin PreRender
    > -> OnPreRender
    > End PreRender
    > -> SaveViewState
    > Begin SaveViewState
    > -> SaveViewState
    > End SaveViewState
    > Begin Render
    > -> RenderContents
    > End Render
    >
    > Is this expected? If so what does it mean?
    >
    > Thanks,
    >
    > Stephen
    Teemu Keiski, Jan 1, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad

    SaveViewState executing twice

    tshad, Aug 25, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    778
    tshad
    Aug 30, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,655
    Smokey Grindel
    Dec 2, 2006
  3. Louis Somers

    SaveViewState called before events are fired

    Louis Somers, Aug 8, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    490
    Louis Somers
    Aug 8, 2008
  4. TS
    Replies:
    5
    Views:
    842
  5. Henry
    Replies:
    6
    Views:
    434
    Guess
    Apr 10, 2005
Loading...

Share This Page