何时在viewstate中保存变量?

时间:2022-06-11 15:15:20

I have searched the web for the answer and saw that mostly variables are saved in viewstate on page.prerender event. Then the value of the variable is set back in page load event.

我在网上搜索了答案,发现大多数变量都保存在page.prerender事件的viewstate中。然后在页面加载事件中设置变量的值。

However, when I save a variable in viewstate on prerender or load events, how can viewstate store the value of the variable after it is changed dynamically on codebehind?

但是,当我在prerender或load事件的viewstate中保存变量时,viewstate如何在代码隐藏后动态更改变量后存储变量的值?

Let's say, after page is loaded, user clicked a button which changes the value of the variable in its onClick event. Then the postback event raised since the button was clicked. According to me, the new value should have been lost and cannot be saved in the viewstate if the variable is saved in the viewstate only in prerender event. Because on postback the prerender event wont fire and the value cannot be saved.

假设在加载页面后,用户单击了一个按钮,该按钮在其onClick事件中更改了变量的值。然后,单击按钮后引发的回发事件。根据我的说法,如果变量仅在prerender事件中保存在viewstate中,则新值应该已丢失并且无法保存在viewstate中。因为在回发时prerender事件不会触发,并且无法保存值。

Shouldn't I save the variable in the viewstate just before the postback event rises?

我不应该在回发事件上升之前将变量保存在viewstate中吗?

Am I wrong? If so, how can viewstate store the new value of the variable if the viewstate is saved in prerender event?

我错了吗?如果是这样,如果viewnate保存在prerender事件中,viewstate如何存储变量的新值?

Thanks for the answer in advance..

谢谢你的回答..

3 个解决方案

#1


5  

I suspect you're confusing saving ViewState, ie. serializing the ViewState collection in memory to a string representation or to an intermediate object that can be serialized easily, with actually modifying that ViewState in-memory object with its regular accessor methods.

我怀疑你在保存ViewState时感到困惑,即。将内存中的ViewState集合序列化为字符串表示或可以轻松序列化的中间对象,实际使用常规访问器方法修改ViewState内存中对象。

What you may be hearing is that the SaveViewState() method is called after PreRender event. But that has little to do with when you get to modify the contents of the ViewState collection.

你可能听到的是在PreRender事件之后调用SaveViewState()方法。但是,当您修改ViewState集合的内容时,这与此几乎没有关系。

Checkout points 7. Prerender the Objects and 8. ViewState Saved in the article The ASP.NET Page Life Cycle. There's a good MSDN article that touches on this as well.

结帐点7.预渲染对象和8. ViewState保存在文章ASP.NET页面生命周期中。有一篇很好的MSDN文章也涉及到这一点。

alt text http://i.msdn.microsoft.com/dynimg/IC152667.gif

alt text http://i.msdn.microsoft.com/dynimg/IC152667.gif

#2


0  

As per ASP.Net Page Lifecycle Overview (emphasis is mine)

根据ASP.Net页面生命周期概述(重点是我的)

PreLoad()

预紧力()

Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request() instance.

在页面加载自身和所有控件的视图状态之后,以及在处理Request()实例包含的回发数据之后引发。

....

....

SaveStateComplete()

SaveStateComplete()

Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.

在为页面和所有控件保存视图状态和控件状态后引发。此时对页面或控件的任何更改都会影响呈现,但在下一次回发时将不会检索更改。

As both Control Events and the PreRender event occur between these two, then your data should be persisted in ViewState.

由于Control事件和PreRender事件都发生在这两者之间,因此您的数据应该保存在ViewState中。

#3


-2  

Viewstate is maintained via a hidden field, so Postback has to finish before it's "set". If you're setting it and trying to read it in the same page cycle it not going to work. You might try using the Session object, which get/sets values into memory.

Viewstate通过隐藏字段进行维护,因此Postback必须在“设置”之前完成。如果您正在设置它并尝试在同一页面循环中读取它,它将无法工作。您可以尝试使用Session对象,它将值/值设置到内存中。

#1


5  

I suspect you're confusing saving ViewState, ie. serializing the ViewState collection in memory to a string representation or to an intermediate object that can be serialized easily, with actually modifying that ViewState in-memory object with its regular accessor methods.

我怀疑你在保存ViewState时感到困惑,即。将内存中的ViewState集合序列化为字符串表示或可以轻松序列化的中间对象,实际使用常规访问器方法修改ViewState内存中对象。

What you may be hearing is that the SaveViewState() method is called after PreRender event. But that has little to do with when you get to modify the contents of the ViewState collection.

你可能听到的是在PreRender事件之后调用SaveViewState()方法。但是,当您修改ViewState集合的内容时,这与此几乎没有关系。

Checkout points 7. Prerender the Objects and 8. ViewState Saved in the article The ASP.NET Page Life Cycle. There's a good MSDN article that touches on this as well.

结帐点7.预渲染对象和8. ViewState保存在文章ASP.NET页面生命周期中。有一篇很好的MSDN文章也涉及到这一点。

alt text http://i.msdn.microsoft.com/dynimg/IC152667.gif

alt text http://i.msdn.microsoft.com/dynimg/IC152667.gif

#2


0  

As per ASP.Net Page Lifecycle Overview (emphasis is mine)

根据ASP.Net页面生命周期概述(重点是我的)

PreLoad()

预紧力()

Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request() instance.

在页面加载自身和所有控件的视图状态之后,以及在处理Request()实例包含的回发数据之后引发。

....

....

SaveStateComplete()

SaveStateComplete()

Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.

在为页面和所有控件保存视图状态和控件状态后引发。此时对页面或控件的任何更改都会影响呈现,但在下一次回发时将不会检索更改。

As both Control Events and the PreRender event occur between these two, then your data should be persisted in ViewState.

由于Control事件和PreRender事件都发生在这两者之间,因此您的数据应该保存在ViewState中。

#3


-2  

Viewstate is maintained via a hidden field, so Postback has to finish before it's "set". If you're setting it and trying to read it in the same page cycle it not going to work. You might try using the Session object, which get/sets values into memory.

Viewstate通过隐藏字段进行维护,因此Postback必须在“设置”之前完成。如果您正在设置它并尝试在同一页面循环中读取它,它将无法工作。您可以尝试使用Session对象,它将值/值设置到内存中。