使用 ViewState 的 Asp.net BasePage 属性在回发时被清除

发布于 2024-07-14 13:44:01 字数 657 浏览 11 评论 0原文

以下是我的设置的基础知识:

我有一个继承自 System.Web.UI.Page 的 BasePage 类。 BasePage 实现了两个属性,以便从 ViewState 集合中的两个不同项目(例如 BasePage 中的 this.ViewState["Year"])获取/设置它们。

我有另一个类,它继承自 BasePage,我们将其称为 SpecificBasePage。

最后,我有一个继承 SpecificBasePage 的 aspx 页面。

我添加了断点并完成了很多代码单步执行,发现在初始页面加载时,我的两个视图状态属性都被分配了值,并且这些值在整个第一页加载生命周期中持续存在。

但是,当页面回发时,在 Page_Load 事件和其他事件处理程序期间(应加载 ViewState 时),两个属性都返回 null。 检查 this.ViewState.Count 显示集合中有零个对象。

有人能想到我可能在某个地方做的事情会影响 ViewState 并导致这种行为吗?

- 添加 我已将其隔离到我的代码的一部分中。 在初始加载时,我在 OnInit 中给出视图状态属性值,我发现当我将其移至 OnLoad 时,这些值在回发过程中持续存在。 我想即使添加的视图状态值在整个初始页面生命周期中持续存在,它们也会在回发中被放弃吗?

Here are the basics of my setup:

I have a BasePage class that inherits from System.Web.UI.Page. BasePage has two properties implemented such that their get/set does so from two different items in the ViewState collection (ex this.ViewState["Year"] from the BasePage).

I have another class that then inherits from BasePage, lets call it SpecificBasePage.

Finally, I have an aspx page that inherits SpecificBasePage.

I have added break points and done much stepping through my code and have found that on the initial page load, both of my view state properties are assigned values and the values persist throughout the first page load life cycle.

When the page is posted back however, during the Page_Load event and other event handlers (when the ViewState should be loaded), both properties return null. Inspecting this.ViewState.Count shows that there are zero objects in the collection.

Can anyone think of something I might be doing somewhere that would effect the ViewState and cause this behavior?

--Addition
I have isolated it to a portion of my code. On the initial load, I give the viewstate properties values in OnInit, I have found that when I move this to OnLoad, the values persist across the post back. I guess even though the added view state values persist throughout the initial page lifecycle, they are abandoned in the post back?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪心 2024-07-21 13:44:01

这些页面可能在 aspx 上具有 EnableViewState = false ,

也可能在应用程序的 web.config 上禁用,甚至在元素上的整个计算机上禁用。

更新 1: 在 asp.net 生命周期中,ViewState 在 Init 之后、Load 更多信息。 就像 init 上的任何内容都被视为页面声明的一部分一样。 稍后,asp.net 将加载视图状态,然后开始跟踪视图状态的更改。 在此之后,asp.net 将看到视图状态上的初始信息和更改后的信息之间的任何差异,因此将其移动到加载可以避免该问题(因为初始状态不存在,新状态是您在视图状态上放置的任何内容) 。 从上面的链接:

“原因是因为StateBag类
只跟踪其成员的变化
在其 TrackViewState() 方法之后
被调用。 也就是说,如果你有一个
StateBag,任何及所有添加或
之前所做的修改
TrackViewState() 被创建将不会
使用 SaveViewState() 方法保存
被调用。 TrackViewState()
方法在最后调用
初始化阶段,发生的情况
在实例化阶段之后。
因此,初始属性
实例化中的赋值
阶段——写入 ViewState 时
在属性的集合访问器中 - 是
期间没有坚持
调用 SaveViewState() 方法
保存视图状态阶段,因为
TrackViewState() 方法尚未完成
调用。”

The pages might have EnableViewState = false on the aspx

Also might be disabled on the web.config of the application, or even for the overall computer on the element .

Update 1: On the asp.net lifecycle, ViewState is loaded after Init, and before Load more info. See it like anything on init is considered part of the declaration of the page. Later asp.net will load the viewstate, and after that it starts tracking changes to the viewstate. It is after this that asp.net will see any difference between the initial information on viewstate and the changed information, so moving it to load avoids the problem (as initial state is not there, and new state is whatever you put on the viewstate). From the link above:

"The reason is because the StateBag class
only tracks changes to its members
after its TrackViewState() method has
been invoked. That is, if you have a
StateBag, any and all additions or
modifications that are made before
TrackViewState() is made will not be
saved when the SaveViewState() method
is invoked. The TrackViewState()
method is called at the end of the
initialization stage, which happens
after the instantiation stage.
Therefore, the initial property
assignments in the instantiation
stage—while written to the ViewState
in the properties' set accessors—are
not persisted during the
SaveViewState() method call in the
save view state stage, because the
TrackViewState() method has yet to be
invoked."

反差帅 2024-07-21 13:44:01

这是另一位用户发布的关于 的出色答案ASP.NET 页面生命周期值得关注一下 ViewState 问题。

另外,请查看“真正了解 ViewState

Here's an excellent answer another user posted about ASP.NET page lifecycle it's worth a look for ViewState issues.

Also, have a look at "Truly Understanding ViewState"

左岸枫 2024-07-21 13:44:01

正如我在稍后添加的部分中所述,显然您无法在页面加载之前在视图状态中设置任何内容,即使 Asp.net 不会抛出错误,并且它将在整个初始页面生命周期中存在于视图状态中,它不会在后续回发的生命周期中持续存在。

As I stated in the part I added later, apparently you can't set anything in the view state before page load, EVEN THOUGH Asp.net doesn't throw an error and it will exist in the view state throughout the initial page life cycle, it will NOT persist on the subsequent post back's life cycle.

最偏执的依靠 2024-07-21 13:44:01

查看您在哪里设置 ViewState 值。 可能连接 OnUnload 事件并检查 ViewState 对象,以确保值确实已设置。

Take a look at where you are setting you ViewState values. Possibly wireup your OnUnload event and inspect the ViewState object just to make sure the values are actually getting set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文