• What is the best practice to save VAR in ViewState in ASP.net

    Abhey Member

    What is the best practice to save VAR in ViewState in ASP.net?

    ViewState["MyClass"] = MyClass;
    Where may I put this one line of code so I am assured it will be its last state?

    I do not want to put it everywhere on the page (Code Behind) that it might be accessed, there should be a place (Event) during the life cycle it could be put so it is after all changes are final.

    I “think” it should be in Page_PreRender, it seems to work, but I cannot find anything (Google) on point, scarry.

    — side issue
    I had quite an issue getting Page_PreRender to fire, found two posts
    1) this.PreRender += new System.EventHandler(this.Page_PreRender);
    private void Page_PreRender(object sender, EventArgs e){…}

    2) protected override void OnPreRender(EventArgs e) {…}

    in attempting to figure out what I may be doing with “override” I deleted it, and everything still worked!
    3) protected void OnPreRender(EventArgs e)

    Which should I use 1, 2, 3, or something else?
    Is Page PreRender the best practice to add a variable to ViewState?

  • Adan Member

    You can store in ViewState or Session variable depend on your requirement.
    ViewState is available for entire page and Session for application.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.