ASP.NET:我怎样才能保持回发之间的对象? [英] ASP.NET : How can i maintain objects between postbacks?

查看:93
本文介绍了ASP.NET:我怎样才能保持回发之间的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何保持ASP.NET页面后背上的对象?

How can i maintain objects between ASP.NET page post backs ?

我有一个ASP.NET page.when我单击One asp.net按钮,我会打电话给我的函数(保存),这将创造我的自定义类(类的UserDetails)的对象,并保存细节DB.so这是back.After后再次在同一页面将显示给user.At那段时间后,我要带我的第一功能(保存)创建的用户对象。什么是做到这一点的最好方法是什么?我知道我可以在这个存储在一个会话和访问它。但我想知道whethere有其他更好的办法?

I have an ASP.NET web page.when i click one asp.net button, I will call my function (Save ) which will create an object of my custom class (UserDetails class) and save the details to DB.so this is a post back.After the post back again the same page will be displayed to user.At that time,I want to take the User object which i have created in the first function (Save ) . What is the best way to do this ? I know i can store this in a Session and access it. But i want to know whethere there is any other better way ?

推荐答案

您正在寻找的方法是某种类型的数据绑定机制,结合对象的值(你可以从数据库加载如果它已经存在的话)您asp.net网络表单。

The approach you're looking for is some kind of databinding mechanism that binds the values of an object (you may load from the db if it already exists) to your asp.net webform.

基本上你具备以下条件:

Basically you'd have the following:


  1. 您显示为您的对象的属性
  2. 字段(即文本框)的空表单
  3. 用户将填写表格,preSS保存

  4. 然后回传会发生。在pageLoad的,你可以检测到这是否符合 Page.IsPostback 回传,如果是这样,您可以创建一个新的对象,并与用户输入的值填充它。

  5. 在您的按钮的OnClick,你调用适当的方法BL存储下来的DB

  1. You display an empty webform with fields (i.e. textboxes) for the properties of your object
  2. The user will fill the the form and press save
  3. Then the postback will happen. On the PageLoad you can detect whether this is a postback with Page.IsPostback and if so, you create a new object and fill it with the values the user entered.
  4. In the OnClick of your button, you call the appropriate BL method for storing it down to the DB

这看起来像(我写出来我的头没有一个编译器,所以要小心:))

This would look like (I'm writing it out of my head without a compiler, so take care :) )

public partial class MyPage : Page
{

    private Person myPersonObj;

    protected void Page_Load(...)
    {

        if(!Page.IsPostback)
        {
            //this is not a postback, so you may load an object based on an ID (i.e. in QueryString or create a new one
            myPersonObj = new Person();
        }
        else
        {
            //it is a postback, so unbind the values
            myPersonObj = new Person();
            Unbind(); //the myPersonObj will be filled, the values are managed by ASP.net in the ViewState
        }

    }

    //caution, overriding Page.DataBind()
    private override void DataBind()
    {
        textBoxFirstname.Text = myPersonObj.FirstName;
        ...

    }

    private void Unbind()
    {
        myPersonObj.FirstName = textBoxFirstname.Text;
    }

    protected void btnSubmit_OnClick(...)
    {
        if(Page.IsValid)
        {
            Save();
        }
    }

    private void Save()
    {
         //ideal layering with Interfaces
         IPersonBL personBL = MyBLFactory.Get<IPersonBL>();
         personBL.SavePerson(myPersonObj); //call the BL for further validation and then persisting
    }
}

我想添加什么昨天,却忘了,因为我不得不赶快:结果
您可以将对象以及存储在ViewState或会话的人描述,但我已经经历了它应该做的尽可能罕见,由于下面的缺点(从我的角度来看):

What I wanted to add yesterday, but forgot since I had to hurry:
You can as well store your objects in the ViewState or Session as others described, but I have experienced that it should be done as rare as possible due to the following "disadvantages" (from my point of view):


  • 您的对象需要是可序列化

  • 在ViewState中存储将drammatically增加你的页面的大小,从而放慢您的网页的加载。注意ViewState是传输到客户端,每个回传的发生时间备份。如果这是唯一的可能性,您遇到性能问题,你可以考虑的尝试这种(但是这应该是例外!)

  • 在您的会话存储对象可能使负载在服务器端,占用内存那里。你应该在你的会话存储对象,可能还在乎这些对象的破坏,如果你知道你不需要他们的任何多加小心。

  • Your objects need to be serializable
  • Storing in the ViewState will drammatically increase the size of your page and thus slowing down the loading of your page. Note the ViewState is transferred to the client and back each time a "postback" occurs. If that's the only possibility and you're experiencing performance problems you may consider trying this (but this should be the exception!!)
  • Storing Objects in your session may put the load on the server-side, consuming the memory there. You should be careful in storing objects in your session and possibly also care about destruction of those objects if you know you don't need them any more.

我所描述的绑定的方式的好处是,你没有这些问题,与每一次有新的新鲜对象的缺点。因此,你必须采取的处理你的对象状态,即手动保持的ID通过往返等护理。

The advantage of the "databinding" approach I described is that you don't have these problems, with the "disadvantage" of having a new fresh object each time. You therefore have to take care of handling your object state, i.e. manually keeping id's through roundtrips etc..

这篇关于ASP.NET:我怎样才能保持回发之间的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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