简体   繁体   English

在ASP.NET应用程序中的哪里存储临时数据

[英]Where to store temporary data in an ASP.NET application

In my web application I have a number of custom grids on a page. 在我的Web应用程序中,页面上有许多自定义网格。 I want the user to be able to edit data in each grid and then press save once at the end of the process to commit their changes. 我希望用户能够编辑每个网格中的数据,然后在过程结束时按一次保存以提交他们的更改。

So I need somewhere temporary to persist their changes before they are comitted. 因此,我需要一个临时的地方来保留它们的更改,然后再提交。 The grids will use AJAX/Javascript so as to not force a full page refresh when data is edited 网格将使用AJAX / Javascript,以便在编辑数据时不强制刷新整个页面

I can think of the following options for storing temporary data 我可以想到以下用于存储临时数据的选项

  1. Hidden form fields 隐藏的表单域
  2. ViewState ViewState
  3. Session 届会

The option I'm least comfortable with is the ViewState option as I have read that this is being phased out in ASP.NET 4.0 and am not sure if you can access ViewState from an AJAX page method. 我最不满意的选项是ViewState选项,因为我已经知道它已在ASP.NET 4.0中逐步淘汰,并且不确定是否可以从AJAX页面方法访问ViewState。

I'm interested in peoples opinions on the above options and also if ViewState should be used for storing data that is not related to out of the box controls. 我对人们对以上选项的意见以及是否应使用ViewState存储与开箱即用控件无关的数据感兴趣。

ASP.Net 4.0 isn't phasing out ViewState (it's pretty central to how web forms work!), it's just slightly altering how ViewState works by default. ASP.Net 4.0并没有淘汰ViewState(它对于Web表单的工作非常重要!),只是稍微改变了ViewState的默认工作方式。 You can read about it here . 你可以在这里阅读

If you want to manually track the data, then the best option is Session. 如果要手动跟踪数据,则最佳选择是会话。 But ViewState is meant for saving these kinds of temporary changes for you automatically. 但是ViewState旨在自动为您保存此类临时更改。

ViewState is not being phased out, and never will be. ViewState 不会被淘汰,而且永远不会被淘汰。

However, you're right that you can't access it in a static page method. 但是,很正确,您不能以静态页面方法访问它。

I recommend that you store the data in the session. 我建议您将数据存储在会话中。

From a server-side technology, session/cache is your best bet; 从服务器端技术来看,会话/缓存是最好的选择。 session is user-specific while cache is application-specific, but you can store items in the cache with a key that has the user id appended to it so that it becomes user specific. 会话是特定于用户的,而缓存是特定于应用程序的,但是您可以使用附加了用户ID的密钥在缓存中存储项目,从而使其成为特定于用户的会话。

On the client-side, you have to use hidden fields to store data because JavaScript doesn't know about session/cache, and AJAX requests don't necessarily have access to certain web resources. 在客户端,您必须使用隐藏字段来存储数据,因为JavaScript不了解会话/缓存,并且AJAX请求不一定具有对某些Web资源的访问权限。 You can try to access these resources through HttpContext.Current in a web request, but information about the page wouldn't be available in a Web Service call or PageMethods web service call. 您可以尝试通过Web请求中的HttpContext.Current访问这些资源,但是有关该页面的信息在Web Service调用或PageMethods Web服务调用中将不可用。

I'm using ViewState and it works just fine. 我正在使用ViewState,并且效果很好。 Session will cause a lot of memory consume and it's not a preferred solution for me while ViewState in essence is just a hidden field so it can be read whenever the page is postback or call back. 会话将导致大量内存消耗,对于我来说,这不是首选的解决方案,而ViewState本质上只是一个隐藏字段,因此只要页面回发或回调时都可以读取它。

My normal process is: - On page load: bind data source to the grid, save the data source to ViewState - On grid submit: + Update: get the update key, extract the data item in data source and update it with latest value + Insert: create a new data item in data source + Delete: mark the data item is deleted with your custom flag Then rebind the grid. 我的正常流程是:-在页面加载中:将数据源绑定到网格,将数据源保存到ViewState-在网格上提交:+更新:获取更新键,提取数据源中的数据项并用最新值更新+插入:在数据源中创建一个新的数据项+删除:使用您的自定义标志标记该数据项已删除,然后重新绑定网格。 - On page saving: get the data source back from ViewState and batch insert/update/delete it into database using your flag -页面保存:从ViewState取回数据源,并使用您的标志将其批量插入/更新/删除到数据库中

As has been said, store it in session but make sure to clean up when you're done and only store what you really need to store, ie, don't just dump a bunch of stuff in session because it's easy. 如前所述,将其存储在会话中,但是请确保在完成后进行清理,仅存储您真正需要存储的内容,即,不要在会话中仅丢弃一堆东西,因为这很容易。

We use session fairly heavily in our app but make sure to clean up after ourselves and only keep what we really need. 我们在应用程序中相当频繁地使用会话,但请确保自己清理后只保留我们真正需要的内容。 You can run into performance issues if you use session as a dumping ground. 如果将会话用作垃圾场,则会遇到性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM