将DataTable分配给ViewState是一个很好的方法? [英] Assigning DataTable to ViewState is a good way?

查看:464
本文介绍了将DataTable分配给ViewState是一个很好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从DataBase获取一个DataTable,并分配给ViewState,如下所示:因为我不想每次都打数据库。

  DataTable dt = GetDataTable(); 
ViewState [dtTable] = dt;

GetDataTable()是一种方法,从DataBase中检索1000条记录。
这是最好的方法还是最好的方法?

解决方案

除此之外,它仍然取决于您的需求,环境设置...



viewstate存储在呈现为< input的隐藏字段中发送到浏览器的最终HTML中的/> 标签。当用户启动回发(按钮点击等)时,数据将作为提交的表单数据的一部分发送回服务器。



如果您存储大量数据的ViewState中的数据,当用户尝试下载页面时,您将不得不承担罚款,因为所有这些数据都将成为您的HTML的一部分,并且当用户尝试提交表单时,再次将该数据发送回服务器。



此外,ViewState容易丢失。只要用户提交表单,它才被保留。如果用户单击超链接到另一个页面,则表单不会被提交,因此ViewState中包含的数据将丢失。



如果数据相对较少,建议使用ViewState。



如果我们考虑安全选项,ViewState数据将以base64编码,可以轻松解码。这是一个黑客网站的典型例子,因此请仔细检查您正在存储的数据。虽然您可以通过将 EnableViewStateMac 设置为true来克服此问题。



对于大量数据 ,会话是一个很好的选择。如果您能够检测任何用户是否完成了特定的数据块,则将会话变量设置为空,以计算内存开销。你不能总是这样做,而且会话将会过期,内存将被自动回收。降低会话超时也可以根据需求进行设置。



此外,会话中的数据实际上在页面加载之间存在于Web服务器上。这有助于保持页面大小,只需使用会话ID。



最后一个选项是使用Caching.Check MSDN这里有关缓存的最佳做法。


I'm getting a DataTable from a DataBase and assigning to ViewState like below: Because I don't want to hit the database for every time.

DataTable dt = GetDataTable();
ViewState["dtTable"] = dt;

GetDataTable() is a method, which retrieves 1000 records from the DataBase. Is this the best way or which one is the best way to handle this?

解决方案

First things first: Explanations aside, It still depends a lot on your requirements, environments settings ...

The viewstate is stored in a hidden field rendered as <input /> tag in final HTML sent to browser . When the user initiates a postback ( Button Click etc..), the data is sent back to the server as part of the submitted form data.

If you store large amounts of data in the ViewState, you will have to incur a penalty when the user attempts to download the page because all such data will be part of your HTML and also when the user attempts to submit the form because again this data will be sent back to server.

In addition, the ViewState is easily lost. It is only preserved as long as the user is submitting the form. If the user clicks a hyperlink to another page, the form is not submitted and therefore data contained within the ViewState is lost.

It is recommended to use ViewState is if the data is relatively small.

If we consider security options, ViewState data is encoded in base64 , which can be easily decoded. This is a classic example for hacking a website, so crosscheck as to what data exactly you are storing. Although you can overcome this issue by setting EnableViewStateMac to true.

For large amounts of data, Session is a good option. If you are able to detect when any user is done with a particular block of data, set the Session variable to null, to counter memory overhead. You can't always do this, but Also the Session will expire and the memory will be reclaimed automatically. Lowering the Session timeout can also help but set it as per the requirement needs.

Also,the data in the Session is actually present on the web server between page loads. This helps in keeping the page size small, it only has to utilize the Session ID.

One last option is to use Caching.Check MSDN here for the best practices on Caching.

这篇关于将DataTable分配给ViewState是一个很好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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