在页面加载时加载视图状态,在页面卸载时保存(来自基类) - c# Asp.net
请原谅我问了一个平凡的新手问题,但我似乎陷入了班级生命周期的困境。
所以我有我的页面
public partial class DefaultPage : BasePage
{
...
}
和基本页面,如下所示:
public class BasePage : System.Web.UI.Page
{
private Model _model;
public BasePage()
{
if (ViewState["_model"] != null)
_modal = ViewState["_model"] as Modal;
else
_modal = new Modal();
}
//I want something to save the Modal when the life cycle ends
[serializable]
public class Model
{
public Dictionary<int, string> Status = new Dictionary<int, string>();
... //other int, string, double fields
}
public class PageManager()
{ //code here; just some random stuff
}
}
现在我只想在页面加载时获取模态,这是我从构造函数中执行的。 如何在页面卸载时保存它?我不能使用析构函数,因为它不可靠。
这种情况的最佳解决方案是什么?
谢谢。
Pardon me for asking a mundane newbie question but I seem to be stuck in a class life-cycle limbo.
So I have my page
public partial class DefaultPage : BasePage
{
...
}
And the BasePage like this:
public class BasePage : System.Web.UI.Page
{
private Model _model;
public BasePage()
{
if (ViewState["_model"] != null)
_modal = ViewState["_model"] as Modal;
else
_modal = new Modal();
}
//I want something to save the Modal when the life cycle ends
[serializable]
public class Model
{
public Dictionary<int, string> Status = new Dictionary<int, string>();
... //other int, string, double fields
}
public class PageManager()
{ //code here; just some random stuff
}
}
Now I just want to get the Modal on page load, which I do from constructor.
How can I save it on page unload? I can't use destructor as It's not reliable.
What is the best solution to this scenario?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LoadViewState
和SaveViewState
是适合此目的的方法。使用这些方法可保证在尝试从 PostBack 加载值之前已从 PostBack 加载 ViewState,并且在将 ViewState 呈现到输出之前已将必要的值放入 ViewState 中。
LoadViewState
andSaveViewState
are appropriate methods for this.Using these methods guarantees that the ViewState has been loaded from the PostBack before you try loading a value from it, and that you have placed the necessary values into the ViewState before the ViewState is rendered to the output.