Assigning DataTable to ViewState is a good way?

30,039

Solution 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.

Solution 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

Share:
30,039
thevan
Author by

thevan

Software Engineering Senior Analyst at Accenture Solutions Private Limited, Chennai, India. Interested in ASP.Net, MVC, Web API, WCF, Web Services, ADO.Net, C#.Net, VB.Net, Entity Framework, MS SQLServer, Angular.js, JavaScript, JQuery, Ajax, HTML and CSS

Updated on April 23, 2020

Comments

  • thevan
    thevan about 4 years

    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?