DataGrid in ASP.NET Common Error (3)

zhaozj2021-02-16  77

Continuous use of large ViewState DataGrid controls to add a lot of viewState in the page, because this will result in a sharp increase in the overall size of the page presenting to the user. To make the page size does not increase, the easiest way is to disable the viewState regardless of the entire page or separately, for some specific controls. For example, if the page does not have a return, disable viewState is safe for the entire page. Otherwise, please invoice for each control between twice farten without changes, or those controls that do not need to hide the field to track the status. When the DataGrid control or page containing the DataGrid is disabled, if the DataGrid starts the return event, then some special steps are required. First, you must resize DataGrid in Page_Load every time you return. This has a violation of a routine approach (and the description in the second problem mentioned above). But if viewState is disabled, this step is required so that other DataGrid events can be correctly raised after executing Page_Load. If you want to handle any of the following DataGrid events (or all), you also need to manually store some DataGrid properties in ViewState. OE Table 1: DataGrid Events and ViewState's dependencies

Does the event depend on ViewState? ItemCreated field to be stored in ViewState no ItemDataBound no SortCommand is SortExpressionEditCommand is EditItemIndex PageIndexChanged is no CurrentPageIndexSelectedIndexChanged Listing 1: enable editing, sorting and paging, but the example code disables the Datagrid ViewState.

Sub Page_Load If Not ViewState ( "EditItemIndex") Is Nothing Then Datagrid1.EditItemIndex = ViewState ( "EditItemIndex") End If If Not ViewState ( "CurrentPageIndex") Is Nothing Then Datagrid1.CurrentPageIndex = ViewState ( "CurrentPageIndex") End If BindGrid ( ) End SubSub BindGrid () Dim DV As DataView DV = GetDataSource () DV.Sort = ViewState ( "SortExpression") Datagrid1.DataSource = DV Datagrid1.DataBind () End SubSub Datagrid1_SortCommand (s As Object, e As DataGridSortCommandEventArgs) ViewState ( " SortExpression ") = e.SortExpression BindGrid () End SubSub Datagrid1_EditCommand (s As Object, e As DatagridCommandEventArgs) Datagrid1.EditItemIndex = e.Item.ItemIndex ViewState (" EditItemIndex ") = e.Item.ItemIndex BindGrid () End SubSub Datagrid1_PageIndexChanged ( s as Object, e As DataGridPageChangedEventArgs) Datagrid1.CurrentPageIndex = e.NewPageIndex ViewState ( "CurrentPageIndex") = e.NewPageIndex BindGrid () End Sub when using ItemDataBound or ItemCreated event ListItemType Datagrid forget to check the proper control data for each row initiator Two events. When each line is added to the DataGrid, Itemcreated events will be triggered, and the itemDatabase event will be triggered when the data is bound to each line. These events can be used to control the appearance or content of each cell when adding a cell to the table output of the DataGrid. For example, the background color of the cell can be modified based on the range of values. But the key is to remember that these incidents are for all DataGrid project types, including header, footer, and paging program projects. If you execute the itemDatabase event, you do not check the project type before the data is referenced, and the first item (usually the title line) will have an error. If the DataGrid is enabled, the first item will become a paging program project in the top display. The following sample code shows how the correct ListItemTYPE check is performed before reference to project data. Don't forget ALTERNATIITEM!

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

New Post(0)