ASP Best practice to save var in ViewState

I have a var that is altered by several processes dependent on the users actions.  Is there a place where I can save this variable at the last pico second :-), just before page unloads, or after everything has finished changing.  I tried OnSaveStateComplete but it is too late.  Is there a correct event I c an use, or something?

Thanks
Sam
SamCashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaydev RathodSr. Software EngineerCommented:
You can store in ViewState or Session variable depend on your requirement.
ViewState is available for entire page and Session for application
0
SamCashAuthor Commented:
Jaydev,

Thanks for the response.

Further clarification...

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?

Kind Regards
Sam
0
SamCashAuthor Commented:
Update, #3 did not work, "override" is required, sorry.
0
MlandaTCommented:
I use PreRender for such things.Technically Page_PreRender is your last chance to tweak the page before it turns into HTML stream.
protected void Page_PreRender(object sender, EventArgs e)
    {
        ViewState["MyClass"] = MyClass;
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SamCashAuthor Commented:
MlandaT,

Thanks for sharing your experience!

Best Regards
Sam
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.