Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
Applications are fundamentally about gathering, displaying, and manipulating data. The ASP.NET data controls provide the means for easily binding to a data source to create data-driven applications. Data-binding is used to populate lists for user selections as well as for displaying tabular sets of data.
-
Use data-binding to declaratively control the display of values combined from the value of other controls.
-
Avoid unnecessary data-binding. If the control has ViewState enabled, it needs to be data-bound only for initialization and then again when the underlying data source changes. However, if you disable ViewState for the control or for the page, you must call DataBind on every page load to repopulate the items.
-
Add dynamic items after data-binding. When adding items to a DropDownList control from both a data source and code in the page, be aware of the order in which they are added. The data-binding operation clears all the items that exist in the control and replaces them with the set from the data source. After DataBind is called, you can safely add items to the list. A good example of where this tip can prove valuable is when you want to add a default value of “Select One” at the top of a list.
-
When ViewState is enabled, as it is by default, the data from the data source makes a round trip between the client and server on each request. When the data is significantly large, this round trip can have a negative impact on the user experience. Look at the size of the ViewState for the DataGrid control by using tracing (discussed in Chapter 10) to understand exactly what kind of impact the ViewState is having on page size.
-
If ViewState size is problematic for an application, a couple of solutions are available. First, you can completely disable ViewState in configuration for a page or for an individual control. Without ViewState, the DataGrid control can no longer automatically store information about the paging state, the item being edited, or the current sort expression. You would need to maintain this data separately. Second, you can simply reduce ViewState by following these steps:
-
Set AutoGenerateColumns to false and explicitly declare only those columns that are necessary.
-
Set the styles in the code explicitly instead of establishing them declaratively.
-
Do not use the DataKeyField member.
-
-
For frequently accessed but rarely changed data, use the application cache or partial page caching (discussed in Chapter 6) to cut down on trips to the database.
-
Custom paging of the DataGrid can be particularly beneficial when the entire set of data being used is quite large or when retrieving it is expensive. Custom paging can also be used when the data source is a DataReader but you still require paging support on the DataGrid. In this scenario, you are responsible for managing the paging in the code, but you might achieve a better result with custom control than by using a DataSet with automatic paging.
-
Use two DataGrid controls on a page to allow for a master view and a details view. Synchronize the SelectedItemIndexChanged event in the master DataGrid to update the contents of the details view DataGrid.
-
Consider providing for data filtering and sorting without return trips to the database for throughput. Be aware, however, that storing data in Session has an impact on the amount of memory used on the server. Also, using ViewState to enable the data to make a round trip has an impact on the size of the page and the post data that will be submitted in subsequent requests.
-
Reflecting on the individual items has an impact on performance. For large sets of data with numerous columns to summarize, leveraging the database directly for calculated values might be more efficient.
Категории