0
votes

In this article http://www.devx.com/vb2themax/Tip/18714 "Improve performance with the EnableViewState property" I can't see where they do set EnableViewState to false. They just do

If Not Me.IsPostBack() Then
...
DataGrid1.DataBind()

Is Postback same as ViewState ?

Why not do EnableViewState = false on datagrid control ?

2
It is assumed that this is set in the .aspx file? - user1429080

2 Answers

0
votes

The article clearly states, that the first example (the part you're quiting from) ist usually done with the "ViewState" enabled. Since only the first get-request needs to bind the grid to the data source (IF Not Me.IsPostBack()), because the consecutive postbacks will restore the grids data from the saved ViewState.

The second example for the Page_Load method assumes that the ViewState has been disabled in the .aspx file and binds the grid to the data source every time the page is loaded.

The call to Me.IsPostBack only checks if the form was accessed via http post, while ViewState is an object which saves all the information concerned with the visual representation of a ASP.NET server control. So, no, they are not the same!

0
votes

Grid controls act as containers for a lot of controls. Each control stores a lot of information in the ViewState.

The ViewState by default is stored in a hidden control. This means that it is sent to the client on every page request. To avoid this overhead, you can set the EnableViewState to false.

<asp:GridView ID="GridView1" runat="server" EnableViewState="false">

ViewState and PostBack are two totally unrelated entities. ViewState stores any information that a control wants to retain. A PostBack only refers to a user sending information back to the page.

However ViewState is also very useful to remember values entered by a user on the last PostBack. If your ViewState is very large, and you don't want to send it to the user, consider saving it in the Session. You can do this by setting the PageStatePersister to SessionPageStatePersister

The SessionPageStatePersister utilizes server sessions associated with the browser session to store the data. Using session instead of a hidden field avoids an increase in the page size being sent to and from the browser. In many situations, the page state is a significant part of the overall markup. However, storing this data in session consumes valuable server resources.