存储在ViewState中的数据表 [英] Storing datatable in ViewState

查看:264
本文介绍了存储在ViewState中的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从微软阅读这篇文章对国家的管理。

<一个href=\"http://msdn.microsoft.com/en-us/library/75x4ha6s(v=vs.100).aspx\">http://msdn.microsoft.com/en-us/library/75x4ha6s(v=vs.100).aspx

我在这里发现了一个有趣的事情。 ViewState中被归类为客户端选项(虽然我已经知道了)。它提醒我们在应用code的我。

  DataTable的DT = getDatatableFromDB();
的ViewState [的dataTable] = DT;

这code是目前工作的罚款。

我的困惑是:


  1. 如何可以在客户端对象(ViewState中)保存到服务器端的对象(数据表)?

  2. 这是一个好的做法是使用ViewState中存储数据表一样大的物体?

  3. 可能可能是什么副作用(如果有的话),如果我们继续使用这种方法?


解决方案

该视图状态保存在一个隐藏的&LT;输入/&GT; 在表单上的标签。当用户发起回传(通过点击一个按钮,例如),该数据被返回到服务器作为表单数据的一部分。

如果你存储大量的数据在ViewState中,你会招致罚款既当用户试图下载页面(因为所有的数据将是HTML的一部分),当用户试图提交表单(因为所有的数据必须被上传到服务器)。

在此外,ViewState是容易丢失。它只有preserved只要用户正在提交的形式。如果用户点击一个超链接到另一个网页,形式从未提交和包含的所有视图状态中的数据丢失。这是真实的,即使在锚定标记指回用户当前的页面。

我从你的 $ P $看到pvious 的问题,你正在努力寻找一个好地方,把你的数据表。 ViewState中并不是最差的地方,只要数据是比较小的。 Base64是比XML在内存使用方面更好,但它仍然是有效的,从很长的路要走。如果数据是相当静态的,你可能要考虑将其存储在 ApplicationState 。如果您正在使用的GridView编辑数据表,然后在GridView实际上已经储存了你的数据表,你可以通过<一个访问href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedataboundcontrol.datasource.aspx\">DataSource财产(只是将它转换回一个DataTable)。


这也是值得注意的是,虽然ViewState的数据连接codeD以base64(指普通用户将无法理解它),它可以很容易地确定的用户编辑。看似平淡无奇的数据可以被编辑成为相当有害的到您的网站。这是黑客攻击网站的经典途径,所以你必须非常小心什么样的数据,究竟要存储。例如,如果您存储用户的ID在ViewState,用户可以编辑该ID和侵入其他用户的帐户。 (注:这是只有EnableViewStateMac已被设置为一个问题 的。)

I was reading this article from Microsoft about the state management.

http://msdn.microsoft.com/en-us/library/75x4ha6s(v=vs.100).aspx

I found an interesting thing here. ViewState is categorized as Client Side option (Although I already knew that). It reminds me of our code in the application.

DataTable dt = getDatatableFromDB();
ViewState["dataTable"] = dt;

And this code is working fine at the moment.

My confusion is :

  1. How can a client side object(ViewState) save Server side object(Datatable)?
  2. Is it a good practice to use ViewState for storing large objects like Datatables?
  3. What possibly could be the side effect(if any) if we keep on using this approach?

解决方案

The viewstate is stored in a hidden <input /> 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.

If you store large amounts of data in the ViewState, you will incur a penalty both when the user attempts to download the page (because all that data will be part of the HTML) and when the user attempts to submit the form (because all that data must be uploaded back to the 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 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.

I see from your previous question that you are trying to find a good place to put your DataTables. ViewState is not the worst place as long as the data is relatively small. Base64 is better than XML in terms of memory usage but it is still a long way from efficient. If the data is fairly static, you may want to consider storing it in the ApplicationState. If you are editing the DataTable with a GridView, then the GridView is actually already storing the DataTable for you which you can access via the DataSource property (just cast it back to a DataTable).


It is also worth noting that while the ViewState data is encoded in base64 (meaning the average user will not be able to understand it), it can be easily edited by a determined user. Seemingly innocuous data could be edited to become quite harmful to your website. This is a classic avenue for hacking a website, so you must be very careful about what data, exactly, you are storing. For example, if you store the user's ID in the ViewState, the user could edit the ID and hack into another user's account. (Note: this is only an issue if EnableViewStateMac has been set to False.)

这篇关于存储在ViewState中的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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