Remove the ViewState in the DataGrid useless data

  In Asp.Net, we use the most is probably the DataGrid Control List.    The function of this control is very powerful and very useful.    It not only can be achieved arbitrary formatting options, but also dynamic paging, sorting, add buttons, dynamic editing functions.    It can be said, has achieved a DataGrid control most functions we need. 

  ViewState DataGrid controls needed to preserve control of the state and if we closed the ViewState, that is, in the HTML code used in the EnableViewState = "False" attribute, then, above all useful functions we will not be able to use. 

  ViewState and shortcomings is a big, will DataGrid is the source of all the data in the data stored in the ViewState.    I tested this, including a 5000 record of the database, the database-if not control the volume of data, direct use Select * From TableName statement this Sql bundled data, an Asp.Net dynamic web page (only a DataGrid Control The test page) ViewState as much as 60 k.    Such a website and put them on public Web sites, slow page download speed so that most people will be discouraged from using the Internet 56 k cat people more not to mention, it is unable to visit the website, the waiting time will not be allowed to endure. 

  ViewState DataGrid stored in the data is divided into two parts, is the preservation of part of the index used, and DataItems DataKeys is the use of data attributes, we call it indexing data.    DataGrid is also part of the contents of the data sources, we call on the data. 

  If in fact we have a list of useless data from the ViewState removed, and this may significantly reduce the size of data ViewState pages, the use of ViewState DataGrid when too much data is the fundamental reason why the list of data stored in the ViewState. 

   DataGrid internal work processes: data binding in the initialization time, a generation is DataGridTable controls object, and this object is inherited System.Web.UI.WebControls.Table controls.      Moreover, this is the first object by adding (using Controls.Add () method) in the DataGrid.      And ViewState in the DataGrid Control List this data is added to the DataGrid.      In fact, the ViewState in the DataGrid cells in the data is actually System.Web.UI.WebControls.Table Control SaveViewState () method added.      These data under many circumstances is unnecessary.  

?

  Solution: in the data binding, set in DataGridTable DataGrid Control EnableViewState property to False on it. 

?

  First of all pages in the initialization of the InitializeComponent () method to add commissioned by the incident: 

  Private void InitializeComponent () 

  ( 

  ?? This.myDataGrid.ItemDataBound + = 

  New DataGridItemEventHandler (this.myDataGrid_ItemDataBound); 

  ) 

  Then myDataGrid_ItemDataBound code control methods include: 

  Private void myDataGrid_ItemDataBound (object sender, DataGridItemEventArgs e) 

  ( 

  ?? MyDataGrid.Controls [0]. EnableViewState = false; 

  ) 

  Use DataGrid, the above code to be reduced when using ViewState DataGrid 90% of the volume of data.    Moreover, the use of ViewState DataGrid many of the functions of the slightest lot, does it mean that the best of both worlds? 

?

  Of course, the implementation of a website, the content of normal DataGrid, and the use of the above methods to remove ViewState, and the back pages if dealt with the contents of DataGrid will disappear.    I understand the DataGrid DataGridTable data stored in the ViewState intention.    Microsoft's design is very stringent, and the intention is that when they use Page.IsPostBack attributes, only a visit to the permanent database can be maintained DataGrid data (not leave this page in the circumstances), the location data is stored pages in the ViewState.    After the back pages of this, DataGrid can be re-created from the ViewState in the DataGrid contents of the display without access database.    So Microsoft at the expense of customers download speed (ViewState data) to ensure that the server resources, we all know that the frequent visits of the database server resource consumption great. 

  Therefore, the use of the above reduction DataGrid ViewState data法子is feasible, but it must be to enable all pages of data processing must be binding, otherwise DataGrid lack of access to the content of the database, they can not access data stored in the ViewState, then back After the DataGrid will not display any content. 

?

  Summary: ViewState reduce the use of the above methods can greatly accelerate client download speed display, but the frequent visits of the database server will increase the pressure on the use of ViewState can reduce the pressure on the server, but they increased client download time, they are each other contradictory.    Therefore, according to the developer to choose whether or not to use the actual situation DataGrid.Controls [0]. EnableViewState = false; the法子, how to choose, we requested their own discretion. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “Remove the ViewState in the DataGrid useless data”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.