C# 将 DataTable 分配给 ViewState 是一个好方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18821957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning DataTable to ViewState is a good way?
提问by thevan
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.
我从数据库中获取数据表并分配给 ViewState,如下所示:因为我不想每次都访问数据库。
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?
GetDataTable()
是一种方法,它从数据库中检索 1000 条记录。这是最好的方法还是哪种方法是处理这个问题的最好方法?
采纳答案by R.C
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.
视图状态存储在一个隐藏字段中,<input />
在发送到浏览器的最终 HTML 中呈现为 标记。当用户启动回发(按钮单击等)时,数据将作为提交表单数据的一部分发送回服务器。
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.
如果您在 ViewState 中存储大量数据,当用户尝试下载页面时,您将不得不受到惩罚,因为所有这些数据都将成为您的 HTML 的一部分,并且当用户尝试提交表单时,又因为这些数据将被发送回服务器。
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.
此外,ViewState 很容易丢失。只要用户提交表单,它就会被保留。如果用户单击指向另一个页面的超链接,则不会提交表单,因此包含在 ViewState 中的数据将丢失。
It is recommended to use ViewState is if the data is relatively small.
如果数据比较小,建议使用ViewState。
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.
如果我们考虑安全选项,ViewState 数据以 base64 编码,可以轻松解码。这是黑客入侵网站的经典示例,因此请交叉检查您究竟存储了哪些数据。虽然您可以通过设置EnableViewStateMac
为 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.
对于大量数据,Session 是一个不错的选择。如果您能够检测到任何用户何时完成了特定的数据块,请将 Session 变量设置为 null,以抵消内存开销。你不能总是这样做,但会话也会过期,内存会自动回收。降低会话超时也有帮助,但请根据需求进行设置。
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.
此外,会话中的数据实际上存在于页面加载之间的 Web 服务器上。这有助于保持较小的页面大小,它只需要使用会话 ID。
One last option is to use Caching.Check MSDN here for the best practices on Caching.
最后一个选择是使用缓存。请在此处查看 MSDN 以获取有关缓存的最佳实践。
回答by Ali Baghdadi
ViewState is not a bad way but when on large data it's better to save your data in a Session variable that has an expiry date you can control
ViewState 不是一种糟糕的方式,但是在处理大数据时,最好将数据保存在具有您可以控制的到期日期的 Session 变量中
回答by Sain Pradeep
View State Side effects
查看状态副作用
1)It is serialized into the input value and deserialized on its way back.
1)它被序列化为输入值并在返回的途中反序列化。
2)The viewstate is stored in a hidden tag on the form. When the user initiates a postback (by clicking a button, for example), the data is returned to the server as part of the form data. It could slow things down.
2)视图状态存储在表单上的隐藏标签中。当用户启动回发(例如,通过单击按钮)时,数据将作为表单数据的一部分返回到服务器。它可能会减慢速度。
3)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 never submitted and all the data contained within the ViewState is lost. This is true even if the anchor tag points back to the page the user is currently on.
3)ViewState很容易丢失。只要用户提交表单,它就会被保留。如果用户单击指向另一个页面的超链接,则永远不会提交表单,并且 ViewState 中包含的所有数据都将丢失。即使锚标记指向用户当前所在的页面也是如此。
I would use the ASP.NET Cacheto store these datatables for the following reasons.
由于以下原因,我将使用ASP.NET 缓存来存储这些数据表。
1)Cache has an expiry, which means you can automatically remove it based upon a sliding or absolute expiry timed value
1)缓存有一个过期时间,这意味着您可以根据滑动或绝对过期时间值自动删除它
2)Cache will automatically be removed if the processes memory "pressure" is too high.
2)如果进程内存“压力”太高,缓存将被自动删除。
3)You can make a cached item specific to one user, or global to all users based upon its key
3)您可以根据其键为一个用户创建一个缓存项,或对所有用户全局
回答by Boyd
Using a DataTable would work. However a DataTable is basically a dynamic object and code in the view tends to be difficult to debug.
使用 DataTable 会起作用。然而,DataTable 基本上是一个动态对象,视图中的代码往往难以调试。
I would suggest creating a class or struct that holds the properties you need.Then convert the DataTable to a list instances of this class, and send that to the view instead.
我建议创建一个包含您需要的属性的类或结构。然后将 DataTable 转换为此类的列表实例,并将其发送到视图。
This will make the code more readable, because now any programmer can see what data you are trying to retrieve. And it will prevent you from having to debug the view a lot.
这将使代码更具可读性,因为现在任何程序员都可以看到您尝试检索的数据。它可以防止您对视图进行大量调试。
回答by Niranjan Singh
View State is good way store Data Table in view state
视图状态是在视图状态中存储数据表的好方法
Store data table into viewstate DataTable dt=new DataTable(); ViewState["dttable"]=dt;
将数据表存入viewstate DataTable dt=new DataTable(); ViewState["dttable"]=dt;
And now ViewState data into Datatable
现在 ViewState 数据转换成 Datatable
DataTable dtnin=(DataTable)ViewState["dttable"];
DataTable dtnin=(DataTable)ViewState["dttable"];