Remove the useless data in DataGrid's ViewState

zhaozj2021-02-16  67

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.

DataGrid controls need to save the status of the control via ViewState, if we shut down ViewState, that is, using the enableViewState = "false" property in the HTML code, then all useful features we will not be available.

The shortcomings of ViewState are also very large, that is, DataGrid will store data in all data sources into 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.

DataGrid Save data in ViewState is divided into two parts, part of the save claim, is the data used by DataKeys and DataItems, and we call it index data. There is also part of the content of data source in DataGrid, which we call list data.

If we remove the list data that is actually useless from ViewState, we greatly reduce the data size of page viewState. When using DataGrid, the root cause of the VIEWSTATE data is that the list data is stored in ViewState.

DataGrid's internal workflow: When the data binding is initialized, a control object called DataGridTable is generated. This object is inherited System.Web.ui.WebControls.Table control. And this object is the first to add (using the controls.add () method) DataGrid. And the DataGrid list data in ViewState is also in the DataGrid. In fact, the data in the cell's cell in ViewState is actually the SaveViewState () method of System.Web.ui.WebControls.Table control. These data are not needed in many cases. ? Solution: When the data is bound, setting the EnableViewState property of the DataGridTable control in the DataGrid is FALSE. ? First, join the incident on the initializationComponent () method in the page initialization:

Private vidinitiRizeComponent ()

{

??? this.mydatagrid.ItemDatabase =

New DataGriditeMeventHandler (this.myDataGrid_itemdatabase;

}

Then add control code within myDataGrid_itemdatabase:

Private void mydatagrid_itemdatabase (Object Sender, DataGriditeMeventArgs E)

{

??? mydatagrid.controls [0] .enableviewState = false;

}

When using DataGrid, add the above code to reduce 90% of the data amount of ViewState when using DataGrid. Moreover, many of the DataGrid's functionality is very small, isn't it two full beauty? ?

Of course, the web page is performed for the first time, the content of the DataGrid is normally displayed, and after using the above removal viewState method, if the page is processed, the content of the DataGrid will disappear. I realized that DataGrid DataGridTable puts the data in the ViewState. Microsoft's design is very rigorous, and their intention is to use the page.ispostback property, only access to a database, you can keep DataGrid data (without left this page), the location of the data is the viewState of the page. in. After the page returns, DataGrid can regenerate the display of DataGrid from ViewState without accessing the database. So Microsoft to sacrifice the speed of the customer to ensure the resources of the server, everyone knows that frequent access to the database is very powerful for the server.

So, the method of using it to reduce DataGrid's viewState data is feasible, but must make all pages return processing must be binded, otherwise the DataGrid cannot obtain the database content, and it is impossible to get the data saved in ViewState. After DataGrid will not display anything. ? Summary: Use the way to reduce ViewState can greatly speed up the client's download display speed, but frequent database access will increase the pressure of the server; use ViewState to reduce the server's pressure, but increase the client's download time, they are Contradictory. So developers should choose whether to use DataGrid.Controls [0] .NableViewState = false according to the actual situation, how to choose, please think yourself.

转载请注明原文地址:https://www.9cbs.com/read-15650.html

New Post(0)