Remove method of useless viewState data in DataGrid
1. ViewState in the DataGrid control
In ASP.NET, we use the most probabilistic is the DataGrid list control. The function of this control is very powerful, and it is also very easy to use. It can not only achieve any formatting option, but also dynamically make paging, sort, add button, dynamic editing and other functions. It can be said that the DataGrid control has achieved most of our features we need.
Unfortunately, many of the very practical functions that are mentioned above, mostly need to support the support, that is, the DataGrid control needs to save the control of the control via ViewState. If we turn off the viewState, enableViewState = "in the HTML code False "Properties, then all useful features above we will not be able to use.
The shortcomings of ViewState are also very large, that is, DataGrid will store data in all data sources into ViewState. DataGrid is a list control that stores data in ViewState, including data in all cells (cell) in the list, more hateful, instant we open the paging function, it also puts all the data not displayed The data in the source is placed in ViewState. If the data volume is not controlled in the database, the DataGrid control is actually stored in the ViewState in ViewState in the ViewState. Direct consequences are ASP.NET generated source HTML code to send to the client huge.
Everyone knows that in ASP.NET, ViewState is actually a name / value pair, in order to properly save the ViewState serialization in order to properly sequentially in various encoded web pages, Base64 encoding. This will increase the amount of data of ViewState.
I tried, a database included in 5,000 records, if not control the amount of data in the database, use the SQL statement such as SQL statement like SQL statement, an ASP.NET dynamic page (only one DataGrid control) The ViewState of the test page is more than 60K. And such a web page is placed in public web sites, slow web download speed will make most people look, people who use 56K cats Internet access will not be mentioned, they can't access such a web page, waiting time will not be able to bear.
So most of the developers of ASP.NET development experience, if you don't use the DataGrid's advanced features, you are usually joined by the enableViewState = "false" property, and you are using your own paging control, not using the paging provided by DataGrid. Function, this will cancel DataGrid's ViewState, which greatly reduces the amount of code that the final generated HTML.
Second, some features of ViewState must be used in DataGrid
Nice, many cases we only need DataGrid's display list data, or use your own paging function, you can use EnableViewState = "false" properties, but if we need to use DataGrid dynamic editing, button column, or When you want to access the index item of DataGrid, we must open the DataGrid's ViewState, which will inevitably appear the amount of viewstate data excessive, but we have no way.
I agree with some functional properties that need to use ViewState in DataGrid, which must use ViewState, and these attributes must use: DataGrid.currentpageIndex DataGrid page index
DataGrid.DataKeys Takes the key value of each record
DataGrid.EditItemIndex Editing Index
Collection of DataGrid.Items DataGrid
DataGrid.PageCount DataGrid page number
When using the DataGrid built-in event, the DataGriditeMeventArgs, DataGridCommandeventArgs, DataGridPageChangeDeventargs, which pass the event parameters, is more necessary to use the DataGrid ViewState, otherwise they will not work.
Here I gave a simple example, add a checkbox column in the DataGrid, which makes the key value of the DataGrid when the page is raised. But if you turn off ViewState, this feature cannot be used.
HTML:
Itemtemplate>
asp: templateColumn>
Columns>
Ask: DataGrid>
CODE:
Private void btnsubmit_click (Object Sender, System.Eventargs E)
{
Checkbox CB;
String Str;
For (int i = 0; I { CB = (Checkbox) MyDataGrid.Items [i] .cells [2] .findControl ("mybox"); IF (cb.checked) Str = mydatagrid.datakeys [mydatagrid.items [i] .ItemIndex] .tostring (); } // The STR variable is the database key value selected in the DataGrid. } The above code is used very successful, but if the enableViewState = "false" property is used in the DataGrid, then the above code cannot work at all.