ASP.net 中关于 WebControl 与 UserControl ViewState 的奇怪行为

发布于 2024-10-23 22:11:09 字数 789 浏览 1 评论 0原文

好的,我已经解决了我的问题,但我不知道为什么一种类型的控件有效,而另一种则无效。

场景:我正在开发自定义向导样式页面设计,该设计最初完全使用 C# ASP.Net 4.0 中的 UserControl 完成。您正在查看的步骤是通过控制可见性在服务器端进行控制的。

我的问题:我不喜欢UserControl并希望尽可能多地使用WebControl,尽管不值得我花精力重做整个过程与 WebControl 一样,需要新的控件来扩展功能。

我最初的解决方案:我最初开始根据需要创建WebControl。这工作得很好,直到我距离给定的 WebControl 两步,它失去了它的 ViewState。至少我相信这就是发生的事情。这是一个 CheckBoxList,当我离开 2 步时,ListItems 就消失了。

最终解决方案:重新创建与UserControl相同的控件我始终可以访问CheckBoxList Items

这一切看起来相当合乎逻辑。如果某个控件或其父控件之一不可见,则它不会包含在 ViewState 数据中。我想真正的问题是,为什么 UserControl 有效?

Ok, I've solved my problem but I don't know why one type of control works and another doesn't.

The scenario: I'm working on a custom wizard style page design that was originally done completely with UserControls in C# ASP.Net 4.0. The step that you are viewing is controlled on server side via control visibilty.

My problem: I dislike the UserControls and want to use WebControls as much as possible, though it's not worth my effort to redo the entire thing with WebControls, and new controls are needed to expand the functionality.

My original solution: I initially started creating WebControls as needed. This worked fine until I got 2 steps away from a given WebControl, where it lost it's ViewState. At least I believe that is what happened. It was a CheckBoxList and as soon as I was 2 steps away, the ListItems disappeared.

The final solution: Recreating the same control as a UserControl I always have access to the CheckBoxList Items.

This all seems fairly logical. If a control or one of it's parent's is not visible, it won't be included in the ViewState data. I guess the real question is, why does UserControl work?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文