你如何坚持回发的一类在ASP.NET 3.5 Web窗体项目? [英] How do you persist a class on PostBack in an ASP.NET 3.5 Web Forms project?

查看:142
本文介绍了你如何坚持回发的一类在ASP.NET 3.5 Web窗体项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何坚持上回发一个ASP.Net类?我已经花时间去到数据库,并填写我的值对象在页面加载时开始,所以我怎么能保存这个对象上回发一个优雅的方式?包含自定义对象回发到自己的页面。

How do I persist an ASP.Net class on PostBack? I've already taken the time to go to the database and fill my object with values when the page loads initially, so how can I save this object in an elegant way on a PostBack? The page that contains the custom object posts back to itself.

有关什么是值得我用C#在ASP.NET 3.5 Web窗体应用程序。

For what it's worth I'm using C# in an ASP.NET 3.5 Web Forms application.

推荐答案

有可能是一个更好的方式与3.5要做到这一点,但在2.0中,您可以使用视图状态。只需将对象添加到视图状态,它会自动包含在默认asp.net形式的隐藏字段。然后在回传则可以从视图状态检索。

There may be a better way to do this with 3.5, but in 2.0 you could use the viewstate. Just add the object to the viewstate and it is automatically included in the default asp.net form as a hidden field. Then on the postback you can retrieve it from the viewstate.

要小心它虽然,你的ViewState是包含在的每个页面负载的。最终,我会建议只是从数据库中获取再次对象,并避免了视图状态。你也可以尝试像 memcached的将对象缓存服务器端。

Be careful with it though, your viewstate is included on every page load. Ultimately I would recommend just fetching the object again from the database and avoiding the viewstate. You could also try something like memcached to cache the object server side.

    protected void Page_Load(object sender, EventArgs e)
    {
        if(ViewState["NameOfUser"] != null)
            NameLabel.Text = ViewState["NameOfUser"].ToString();
        else
            NameLabel.Text = "Not set yet...";
    }

    protected void SubmitForm_Click(object sender, EventArgs e)
    {
        ViewState["NameOfUser"] = NameField.Text;
        NameLabel.Text = NameField.Text;
    }

http://asp.net-tutorials.com/state/viewstate/ <例/ A>

Example from http://asp.net-tutorials.com/state/viewstate/

这篇关于你如何坚持回发的一类在ASP.NET 3.5 Web窗体项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆