Advanced Topics On Using ADO - NET in ASP - NET Data Binding

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Advanced Topics on

Using ADO.NET in
ASP.NET Data Binding

Yilei Wang
Developer Support
Microsoft Corporation
Agenda
 Further exploration on the DataGrid
Web server control
 Multiple item selection
 Multiple item editing
 Summary rows
 Master/Detail page
 Web data access strategy

2
DataGrid - Multiple Item Selection
 Functionality: Users select multiple rows by
checking the check box for each row, and
then perform an action on the selected
items.
 Add a template column to the DataGrid, put a
CheckBox control in its ItemTemplate.

3
DataGrid - Multiple Item Selection (2)
 In the code behind, walk through each
DataGrid item to see if the check box is
checked; if checked, perform the action.

4
DataGrid - Multiple Item Editing
 Functionality
 Data is displayed in editable controls by
default.
 Users make changes in multiple items and
then click a button to commit all changes
at once.

5
DataGrid - Multiple Item Editing (2)
 Create template columns for editable data.
 Add editable controls in the ItemTemplate.

6
DataGrid - Multiple Item Editing (3)
 Updating is slightly different in the code
behind.
 When the user clicks the Update button,
there is no way to keep track of which
columns of which rows have been modified.
 Need to go through each DataGrid item,
extract values from all editable controls, and
make updates for all the rows.

7
DataGrid - Multiple Item Editing (4)

8
DataGrid - Summary Rows
 Functionality: DataGrid contains summary
rows to display partial totals of grouped
columns.
 The sample is based on the
Northwind Microsoft® SQL
Server™ database. The
DataGrid lists all the orders
that each customer has
issued in year 1998.
 For each customer, a
summary row displays the
total amount of orders.
An extra row summarizes the
grand total for all customers.
9
DataGrid - Summary Rows (2)
 DataGrid doesn’t have the feature for
summary; you must insert summary rows
directly into the data source before attaching
the data to the grid.
 When rows are being drawn, distinguish
summary rows from ordinary rows, and
render the former with different layout and
style.

10
DataGrid - Summary Rows (3)

 The GROUP BY clause of the SELECT statement in T-SQL


provides the WITH ROLLUP extension that adds predefined
summary rows to the result set.
 GROUPING is the T-SQL aggregate function that works in
conjunction with ROLLUP. The use of the GROUPING operator
causes a new column to be added to the result set. This column
contains a value of 1 if it is grouped by the ROLLUP operator.
Otherwise, the column takes a value of 0.
 By using a CASE..WHEN..END statement you can merge this new
column with the grouping column.
11
DataGrid - Summary Rows (4)

 The result of the query on the previous slide.

12
DataGrid - Summary Rows (5)
 The DataGrid declaration in HTML view.

 In the ItemCreated event handler, detect each


summary row, and modify its layout and style.

13
DataGrid - Summary Rows (6)
 The item text is set only during the
OnItemDataBound event.
 Modifying the text in summary rows must be
done in the OnItemDataBound event
handler.

14
DataGrid - Summary Rows (7)
 The final DataGrid.

15
DataGrid - Master/Detail Page
 Functionality: Users select an item in the
master DataGrid, and the details will be
displayed in another DataGrid.
 Load two tables in the DataSet and create a
DataRelation between them.

16
DataGrid - Master/Detail Page (2)
 The SelectedIndexChanged event is fired on
selecting an item in the master DataGrid.
 In its event handler, get the corresponding
DataRows through the DataRelation, and bind
the rows to the detail DataGrid.

17
Web Data Access Strategy
DataReader or DataSet
 DataReader
 Slightly better performance and memory
usage
 Data object, by default, is discarded with
each round trip—using DataAdapter for
data binding is usually recommended
 Reasons for using DataSet
 Working with related tables
 Exchanging data with other processes
 Working with a static set of records
18
Web Data Access Strategy
Cache DataSet or Reload DataSet
 Caching DataSet
 Pro: saves trips to the data source.
 Con: consumes memory between round
trips; can get out of sync with the data
source.
 Scenarios for reloading DataSet
 Paging through data: reload the DataSet
to get the next set of records to display.
 Working with very volatile data source.

19
Web Data Access Strategy
Cache on Client or on Server
 Caching on the client: Save the DataSet
using view state or by putting the data
into your own hidden field.
 Pro: no server resources are required.
 Con: storing large values can slow down
the page loading and posting back.
 Example:

20
Web Data Access Strategy
Cache on Client or on Server (2)
 Caching on the server: Save the dataset
in Session state, Application state, or
using a cache.
 Session is scoped to the current browser
session.
 Application: global storage accessible
from all pages in the Web application.
 Cache: thread-safe application level
global storage. Cache manager will
remove the least-used items if the server
needs memory or if cached data expires.
21
Web Data Access Strategy
Cache on Client or on Server (3)
 Example for using caching session
 To store the DataSet in the cache:

 To get the DataSet back:

22
Web Data Access Strategy
Save DataSet in XML File
 Doesn’t take up server or client resources
 Takes a relatively long time to serialize and
deserialize data.

23
Additional Resources
 Knowledge Base articles
 Q313481 “INFO: Roadmap for Web Forms Data
Binding”
 Q307860 “INFO: ASP.NET Data Binding
Overview”
 Q305140 “INFO: ASP.NET Roadmap”
 Q313590 “INFO: Roadmap for ADO.NET”
 Other links
 Support WebCast: Microsoft ASP.NET DataGrid W
eb Server Control
 Support WebCast: Data Binding in ASP.NET Web
Forms
24
25

You might also like