在页面加载时加载视图状态,在页面卸载时保存(来自基类) - c# Asp.net

发布于 2024-11-26 23:19:32 字数 894 浏览 6 评论 0原文

请原谅我问了一个平凡的新手问题,但我似乎陷入了班级生命周期的困境。

所以我有我的页面

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 技术交流群。

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

发布评论

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

评论(1

听不够的曲调 2024-12-03 23:19:32

LoadViewStateSaveViewState 是适合此目的的方法。

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        _model= (Model) ViewState["_model"];
    }

    protected override object SaveViewState()
    {
        ViewState["_model"] = _model;
        return base.SaveViewState();
    }

使用这些方法可保证在尝试从 PostBack 加载值之前已从 PostBack 加载 ViewState,并且在将 ViewState 呈现到输出之前已将必要的值放入 ViewState 中。

LoadViewState and SaveViewState are appropriate methods for this.

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        _model= (Model) ViewState["_model"];
    }

    protected override object SaveViewState()
    {
        ViewState["_model"] = _model;
        return base.SaveViewState();
    }

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.

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