简体   繁体   English

如何清除Viewstate?

[英]How to clear Viewstate?

I am having a problem in which two controls which are added programmatically are trying to load each other viewstate, I want to clear the viewstate before loading the controls, I tried Viewstate.Clear but it did nothing, when I disable viewstate on the container of my controls everything works fine except that the control's state is not kept. 我有一个问题,其中两个以编程方式添加的控件正在尝试加载彼此的视图状态,我想在加载控件之前清除视图状态,我尝试了Viewstate.Clear但它没有做任何事情,当我在容器上禁用viewstate时我的控件一切正常,但控件的状态没有保留。 Is there a way to clear viewstate of just a specific control? 有没有办法清除只有特定控件的视图状态?

From your description, it would seem that you are making one of the common mistakes when loading your dynamic controls - either you are loading them too late or you are not assigning them unique IDs (and assigning them the same unique id each time a postback occurs ). 根据您的描述,在加载动态控件时,您似乎犯了一个常见的错误 - 要么加载它们太晚,要么您没有为它们分配唯一ID(并且每次发生回发时都会为它们分配相同的唯一ID) )。

If this is indeed your problem, then clearing the viewstate is not the appropriate action to be taking. 如果这确实是您的问题,那么清除视图状态不是适当的操作。 It is quite simple to fix, check these three links: 修复起来非常简单,请检查以下三个链接:

http://msdn.microsoft.com/en-us/library/ms972976.aspx http://msdn.microsoft.com/en-us/library/ms972976.aspx

http://www.4guysfromrolla.com/articles/092904-1.aspx http://www.4guysfromrolla.com/articles/092904-1.aspx

http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx

Yes , 是的,

string controlName = "name of control";
ViewState[controlName] = null;
// create control, add it to the page

If ViewState gets in your way and you haven't done so already, please read 如果ViewState妨碍了您并且您还没有这样做,请阅读

TRULY understanding ViewSate 真正了解ViewSate

It will make you much more comfortable in working with ViewState and the whole ASP.NET page lifecycle. 它将使您在使用ViewState和整个ASP.NET页面生命周期时更加舒适。

Assuming both controls inherit from System.Web.UI.Control . 假设两个控件都继承自System.Web.UI.Control You can disable their individual ViewStates by setting their EnableViewState to false. 您可以通过将其EnableViewState设置为false来禁用其各自的ViewStates。 Also you can clear their ViewState, as each control has a ViewState property. 您也可以清除它们的ViewState,因为每个控件都有一个ViewState属性。

确保两个以编程方式添加的控件的ID不同,并且ViewState问题应该消失。

您可以在特定控件上禁用viewstate:

EnableViewState="False"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM