locked
Last Page event to modify ViewState and page output RRS feed

  • Question

  • User1904516115 posted

    Which is the last event to modify ViewState?

    Which is the last event to modify page output?

    Saturday, November 11, 2017 6:13 AM

All replies

  • User-707554951 posted

    Hi vinodkpasi,

    Which is the last event to modify ViewState?
     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

    Which is the last event to modify page output?

    Render event.

    protected override void Render(HtmlTextWriter writer)
    {
       StringBuilder htmlString = new StringBuilder(); // this will hold the string
       StringWriter stringWriter = new StringWriter(htmlString);
       HtmlTextWriter tmpWriter = new HtmlTextWriter(stringWriter);
       Page.Render(tmpWriter);
       writer.Flush();
    
       writer.Write(DoReplaceLogic(htmlString.ToString()););
    }

    Best regards
    Cathy

    Monday, November 13, 2017 10:11 AM
  • User1904516115 posted

    PreRenderComplete event also occurs after PreRender. In that case PreRenderComplete will be the last event where we can modify the ViewState. 

    Friday, February 1, 2019 4:52 PM