views:

107

answers:

1

I am about to gone mad why? why? why?

protected void Page_Load(object sender, EventArgs e) { AttachedPartnersViewState vs = ViewState[SessionVariables.Company_AttachedPartnersViewState] as AttachedPartnersViewState;

protected override void OnUnload(EventArgs e)
{
    ViewState[SessionVariables.Company_AttachedPartnersViewState] = _viewState;

whatever I do. however I try to save this Viewstate, it seems to be ok on unload. then, as page is loading, it is NULL again, it DOESN'T retrieves despite page is on postback.

this is elementary action, but why it doesn't work?

BTW, control's EnableViewstate is true

Are there any explanations?

+2  A: 

SaveViewState is before the Render Method. ViewState is encoded and written out to the page to be posted back in. If you add ViewState info after the Render, it is not written back out the the page. Hence it will not be read back in on the postback. Think of ViewState as a hidden input.

Links:

More on Page Lifecycle

More on ViewState

Tim Hoolihan
Thanks I tried to go such way before, but before render it causes tremendous error . It occurs not while passing through line in that I save object to viewstate (debugger passed that line ok), but when page is rendered (it seems so). Exception of type 'System.Web.HttpUnhandledException' was thrown. --- Error serializing value 'MDSWebApp.Controls.AttachedPartners+AttachedPartnersViewState' of type 'MDSWebApp.Controls.AttachedPartners+AttachedPartnersViewState.'
igor
that error means you are trying to save something into viewstate that is not serializable. All data saved out to the page has to be serialized. What are you trying to save in viewstate?
Tim Hoolihan
Thanks, I more or less aware what is nature of this error )I try to save class which functionally is container for carrying my data through postback. It contains only 3 Lists<> and 1 int.But when I do the same via Session instead of Viewstate (just replace ViewState with Session), it serializes and retrieves without errors, that is strange.The only way I see now is use the session instead, though it is not quite logically right at the design point of view.anyway, such behavior of viewstate still be strange and make me warned for its further use,
igor