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. Advertisements

  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, Jan 1, 2004
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.