Assigning DataTable to ViewState is a good way?

点点圈 提交于 2020-01-24 07:27:28

问题


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?


回答1:


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.




回答2:


View State Side effects

1)It is serialized into the input value and deserialized on its way back.

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.

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.

I would use the ASP.NET Cache to store these datatables for the following reasons.

1)Cache has an expiry, which means you can automatically remove it based upon a sliding or absolute expiry timed value

2)Cache will automatically be removed if the processes memory "pressure" is too high.

3)You can make a cached item specific to one user, or global to all users based upon its key




回答3:


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




回答4:


Using a DataTable would work. However a DataTable is basically a dynamic object and code in the view tends to be difficult to debug.

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.

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.




回答5:


View State is good way store Data Table in view state

Store data table into viewstate DataTable dt=new DataTable(); ViewState["dttable"]=dt;

And now ViewState data into Datatable

DataTable dtnin=(DataTable)ViewState["dttable"];



来源:https://stackoverflow.com/questions/18821957/assigning-datatable-to-viewstate-is-a-good-way

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