Using VeiwState on an ASPX page

I have read everything I can get my hands on about ViewState but can't seem to figure out what I'm doing wrong.

In a sub routine that first and foremost fills a grid, I set the value of two controls and then save those to view state:

                    ViewState.Item("SortKey") = SortKey.Value
                    ViewState.Item("RecId") = RecId.Value

I'm using ViewState because I can't seem to get the control values to persist.

On Page_Load I try to retrieve those values into the controls:
            SortKey.Value = ViewState.Item("SortKey")
            RecId.Value = ViewState.Item("RecId")

This does not work.

What am I missing?


Thanks
LVL 4
g_johnsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
On Page_Load I try to retrieve those values into the controls:

SortKey.Value = ViewState.Item("SortKey")
            RecId.Value = ViewState.Item("RecId")
are you doing this in the same page in where you set the viewstate values?
0
g_johnsonAuthor Commented:
Yes, and it's enclosed in an "If postback" block, which I should have made clear.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
it should working fine with ViewState statements you posted above.

how you calling that sub routine? is it before the postback event?
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
g_johnsonAuthor Commented:
You are correct that it should work as written.  We believe that because we are using a proprietary control on the page and that's where ViewState is captured that it's not in a good spot during the life cycle of the page.
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.