将控件序列化为视图状态
我正在编写一个自定义用户控件。我觉得我正在以艰难的方式做这件事。
据我了解,为了在回发之间保留控件的状态,我必须将数据保存到 ViewState。我已经在我的用户控件类中的几个字段中完成了此操作。
然而,这似乎非常乏味。有没有办法让 ASP.net 在页面加载完成后立即将用户控件中的所有可序列化字段保存到 ViewState 中?
I'm writing a custom user control. I feel like I'm doing this the hard way.
I understand that in order for my control's state to be preserved between postbacks, I have to save the data to ViewState. I've done this with several fields in my User Control's class.
However, it seems very tedious. Is there a way to make ASP.net save all the Serializable fields in my User Control to ViewState all at once when the page is done loading?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一个解决方案,您仍然需要根据您的情况进行调整,因为此示例(重新)存储了控件的所有状态,以及 asp.net 运行时设置的属性。请记住,序列化不能在字段的属性上设置,只能在类/结构上设置。但是,您可以创建自己的属性(ViewStateSerialized?),用于装饰要在回发期间保留的属性。请记住,视图状态是通过线路传输到客户端的,因此如果您有很多视图状态,用户可能会感到不安......
I have a solution that you still need to adapt for your situation because this sample (re)stores all state of the control, also properties set by the asp.net runtime. Remember that Serializable is not settable on a property of field, only a class/struct. You can however create your own Attribute (ViewStateSerializable?) that you use to decorate the properties you want to retain during postbacks. Do remember that viewstate is going over the wire to the client so if you have a lot of it users might get upset....