Persist c# objects across postbacks

隐身守侯 提交于 2019-12-10 14:09:14

问题


I've got an asp.net page that has c# code-behind that does some stuff in the Page_Load() method (like query a database and make a few other calls to populate objects with data). I then display this data on the page. This all works fine. I set up a couple of postbacks so that when a value in a listbox is clicked, a panel control is filled with the rest of the corresponding object's data. I thought postbacks were the right way to do this, but this causes the (entire class?) to be re-called, which re-initializes my objects and destroys the data I want to keep.

Will some form of partial-postback solve this problem, or is there a better way to implement what I'm trying to do?

I don't want to re-populate the objects every time a postback is called, as that takes a database query, and I want to avoid re-querying every time something is clicked...

I've found many questions regarding persisting Javascript objects, but nothing that really seems to address this. I'm using .Net 4.0


回答1:


Put the objects into the Session for the current user.




回答2:


Put all your initialization stuff in an (!IsPostback) { } and use partial postbacks. That way the initialization code doesn't get called again during the postbacks.




回答3:


Why don't you cache the object?

Caching API, Using the Cache Object: http://msdn.microsoft.com/en-us/library/aa478965.aspx#aspnet-cachingtechniquesbestpract_topic4



来源:https://stackoverflow.com/questions/6309185/persist-c-sharp-objects-across-postbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!