Escolar Documentos
Profissional Documentos
Cultura Documentos
Main Menu 1 of 61
DataBinding with ListControls
Scope
Introduction to ADO.NET
Binding Data to Controls
List Controls
List Control Base Class
Look-Less Controls
DataList Control
Repeater Control
Table Control
Fetching Data from SQL Stored Procedures
Fetching XML-based Data
Main Menu 2 of 61
DataBinding with ListControls
Introduction to ADO.NET
ADO.NET is designed to be data-centric, non-database-
centric, focused on handling disconnected data sets, and
tightly integrated with XML.
It enables better data sharing capabilities between
components and tiers in an application.
A .NET data provider is used for connecting to a database,
executing commands, and retrieving results.
Those results are either processed directly, or placed in an
ADO.NET DataSet in order to be exposed to the user.
Main Menu 3 of 61
DataBinding with ListControls
Introduction to ADO.NET
The ADO.NET objects are:
Connection Object
Command Object
DataReader Object
DataAdapter Object
DataSet Object
Main Menu 4 of 61
DataBinding with ListControls
Connection Object
The .NET Data Provider Connection object is used
to set a connection to a data source.
Each .NET Provider has its own Connection object.
The SQL Server .NET Provider includes a
SqlConnection Object, while the OLE DB .NET
Provider includes an OleDbConnection object.
The ADO.NET Connection object accepts one string
parameter called ConnectionString for assigning the
data source connection details.
Main Menu 5 of 61
DataBinding with ListControls
Connection Object
Ideally the string for ConnectionString should be
written as follows:
Provider=SQLOLEDB.1; Data Source=MySQLServer; Initial
Catalog=NORTHWIND; uid=sa; pwd=;
For the SqlConnection object this string is like a
typical OLE DB Provider string, except that you omit
the Provider parameter.
Imports System.Data.SqlClient
Dim SqlConn As SqlConnection = New SqlConnection ("
Data Source=tilesd; Initial Catalog=Northwind; uid=SA; pwd= ;")
Main Menu 6 of 61
DataBinding with ListControls
Command Object
The Command object allows executing a SQL
statement or stored procedure in a data source.
On executing the Command object, either a row set
containing data is returned that is passed to another
object such as DataReader/ DataAdapter or it returns
a count of number of records affected for queries
that do not return a row set.
There are two command objects, one for OLEDB
and other one for SQL as given in next slide:
Main Menu 7 of 61
DataBinding with ListControls
Command Object
SqlCommand: Uses Tabular Data Services with MS SQL
Server
OleDbCommand: Used with OLE-DB provider or OLE-
DB/ODBC driver
The syntax of creating an instance of Command
object and how to execute it is given below:
Dim myCommand As New SqlCommand (<SQL
Statement>, <Connection name>)
myCommand.Connection.Open ()
myCommand.ExecuteNonQuery ()
Main Menu 8 of 61
DataBinding with ListControls
DataReader Object
The DataReader object is squarely aimed at web
developers. It provides a data, read-only, forward-
only data stream that is designed to provide quick
access to data.
The DataReader object will retrieve multiple
resultset . It will also return schema information for
the underlying data source.
Each .NET Data Provider includes its own version
of the DataReader object.
Main Menu 9 of 61
DataBinding with ListControls
DataReader Object
Syntax of making an instance of SqlDataReader
object:
Main Menu 10 of 61
DataBinding with ListControls
DataAdapter Object
The DataAdapter object is a very important member of the
Managed Data Provider because it provides the link for
exchanging the data between a data source and a data set.
ADO.NET Provides the DataSet object for caching and
manipulating data.
Data adapters can move any kind of data between a data
source and a DataSet object as long as it is supported by a
.NET Data Provider.
The DataAdapter object is used to retrieve data from a data
source and populate the DataTable objects in a DataSet.
Main Menu 11 of 61
DataBinding with ListControls
DataSet Object
The main advantage of DataSet is that a DataSet object can
hold more than one table from the same source, as well as the
relationship between them.
Each table in a DataSet is a DataTable object within the
DataTableCollection.
Each DataTable object contains a collection of DataRow
objects (within the DataRowCollection) and a collection of
DataColumn objects (within the DataColumnCollection).
Main Menu 12 of 61
DataBinding with ListControls
Main Menu 13 of 61
DataBinding with ListControls
List Controls
List controls are used in conjunction with ADO.NET.
They provide data binding facilities, which allow us to
display data from a particular data store via a control.
We have the facility to bind to various sources such as
arrays and Datasets in ADO.NET.
We can also use templates to change the layout of the
control and display a fully featured list output with
editing ability.
Main Menu 14 of 61
DataBinding with ListControls
List Controls
Types of List Controls:
DropDownList Control
ListBox Control
CheckBoxList Control
RadioButtonList Control
DataGrid Control
DataList Control
Repeater Control
Main Menu 15 of 61
DataBinding with ListControls
Main Menu 16 of 61
DataBinding with ListControls
Adding items
You can add items to a list Web server control in
following ways:
Add static items at design time.
Add items programmatically at run time.
Add items using data binding.
Main Menu 17 of 61
DataBinding with ListControls
Adding items
Adding static items at design time
In Design view, select the list control.
In the Properties window, click the ellipsis button for the
Items property to open the Collection Editor dialog box.
Click Add to add a new item.
To set the properties for the new item, select it, and then in
the ListItem Properties list, enter values for its Text, Value,
and Selected properties.
Keep on repeating Steps 2 and 3 for each item to add, and
then click on OK to close the Collection Edit or Dialog box.
Main Menu 18 of 61
DataBinding with ListControls
Adding items
Adding items programmatically
Drag a list control and set its Text and Value properties.
Call the Add method of the control's Items collection and
pass it to the new object.
The following example shows how to add ListItem objects to
a ListBox control on click of a button,
Public Sub Button1_Click(ByVal sender As Object, ByValue As
System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(New ListItem("Delhi", "DEL"))
ListBox1.Items.Add(New ListItem("Mumbai", "Mum"))
End Sub
Main Menu 19 of 61
DataBinding with ListControls
Adding items
Populating from a database
You can use a list Web server control to display items that
are fetched from a data source.
Each item in the control corresponds to an item (a row) in the
data source.
The control can display one field from the source and you can
also optionally use a second field as the item value.
Main Menu 20 of 61
DataBinding with ListControls
Main Menu 21 of 61
DataBinding with ListControls
Main Menu 23 of 61
DataBinding with ListControls
SelectedItem property.
This property returns an object of type ListItem.
You can get the contents of the selected item by getting the
Main Menu 24 of 61
DataBinding with ListControls
Main Menu 25 of 61
DataBinding with ListControls
Main Menu 26 of 61
DataBinding with ListControls
Main Menu 27 of 61
DataBinding with ListControls
This code snippet selects the third item in the list box.
Main Menu 28 of 61
DataBinding with ListControls
Main Menu 29 of 61
DataBinding with ListControls
ListControl Events
SelectedIndexChanged:
A ListControl raises this event when user selects an item.
By default, this event does not cause the page to be posted to
the server, but you can cause the control to force an
immediate post by setting the AutoPostBack property to true.
Please note that the ability of a DropDownList control to post
to the server when it is checked requires that the browser
support ECMAScript (JScript, JavaScript) and that scripting
be enabled on the user's browser.
Main Menu 30 of 61
DataBinding with ListControls
DropDownList Control
It represents a control that allows the user to select a single
item from a drop-down list.
The DropDownList control is very similar to the ListBox
Web server control. However it shows only the selected item
in a box, along with a drop-down button the list of items
remains hidden until users click the drop-down button.
You can control the appearance of the DropDownList control
by setting the BorderColor, BorderStyle, and BorderWidth
properties.
Main Menu 31 of 61
DataBinding with ListControls
DropDownList Control
Binding Data to the Control
You can use a DropDownList Web server control to list
options that are read from a data source.
Each item in the DropDownList control corresponds to an
item i.e., a row — in the data source.
The control displays one field from the source.
Optionally, you can bind the control to a second field to set
the value of an item.
Main Menu 32 of 61
DataBinding with ListControls
ListBox Control
This control displays a list of items that allows single or
multiple item selection.
The ListBox Web server control allows users to select one or
more items from a predefined list of item displayed in the list
box.
It differs from a DropDownList control as it can display
multiple items at a time.
Main Menu 33 of 61
DataBinding with ListControls
ListBox Control
Binding Data to the Control
You can use a ListBox Web server control to list options that
are read from a data source.
Each item in this control corresponds to an item i.e. a row —
in the data source.
The control displays one field from the source.
Optionally, you can bind the control to a second field to set
the value of an item.
Main Menu 34 of 61
DataBinding with ListControls
Main Menu 35 of 61
DataBinding with ListControls
Main Menu 36 of 61
DataBinding with ListControls
Main Menu 37 of 61
DataBinding with ListControls
Main Menu 38 of 61
DataBinding with ListControls
DataGrid Control
DataGrid displays information stored in the database in a grid
form, such as table containing rows and columns.
This control is used for displaying Full-featured list output
with editing and Paged reporting.
Has a grid appearance by default.
Can customize look of grid extensively.
Has an auto-format option.
Can specify output using bound columns, columns of buttons
or hyperlinks, and custom columns created using templates.
Main Menu 39 of 61
DataBinding with ListControls
DataGrid Control
Has no separator template. However, the grid renders in a
table, and you can specify table border size and color.
By default, the DataGrid displays data in read-only mode, but
the DataGrid is also capable of automatically displaying the
data in editable controls at run-time.
Supports WYSIWYG template editing.
Supports custom functionality that can be applied to items
(for example, specifying an "Add to shopping cart" button for
each item).
Supports single selection. Multiple selection requires custom
code.
Main Menu 40 of 61
DataBinding with ListControls
Properties
DataSource Property:
The DataGrid Web server control is bound to a data source
via this property otherwise it is not rendered (displayed) on
the page.
Typical data sources for the DataGrid are the DataSet and the
OleDbDataReader.
You can use data sources available from the Toolbox, such as
the DataSet or DataView class.
You can also bind to data sources through code, such as
OleDbDataReader or an array.
Main Menu 41 of 61
DataBinding with ListControls
Properties
DataKeyField Property:
As part of the data binding, you can specify a DataKeyField
property.
This property allows you to specify information that uniquely
identifies each item in the grid.
The information does not have to be part of the information
displayed in the grid.
It can consist of the name of a field in the data source (such
as a primary key).
You can use this information to update a specific item in the
data source.
Main Menu 42 of 61
DataBinding with ListControls
Methods
DataBind (inherited from BaseDataList):
This method binds the control and all its child controls to the
data source specified in the DataSource property of the
control.
When the Web page runs, the code must call the control's
DataBind method to load the grid with data (this is true for all
the data bound controls).
This method is called again if the data changes (for example,
in an event handler) to refresh the grid.
Main Menu 43 of 61
DataBinding with ListControls
Property Builder
If you right click on the DataGrid Control there is a “Property
Builder” option, you will find various tabs in it such as
“General”, “Columns”, “Paging”, “Format” and “Borders”.
There is a Format tab in the Property Builder dialog box
which controls the layout of the rows of the DataGrid.
Its settings specify the color, font, and alignment etc. of the
rows.
The actual control, text, and data contents of the row are
specified in the Columns tab of the Property Builder.
Main Menu 44 of 61
DataBinding with ListControls
Property Builder
Sorting
The DataGrid Web server control provides a way for you to
add sorting to the grid.
The grid does not directly support sorting.
It provides a way for you to add sort options to the grid, such
as hyperlinks in the column heads that users click to sort.
The grid then notifies you when a user has requested sorting.
The DataGrid control provides two levels of sorting support:
Default sorting:
Custom sorting:
Main Menu 45 of 61
DataBinding with ListControls
from Emp” .
Drag and drop a DataGrid control on the web form and set the
DataKeyField Emp_id
Main Menu 46 of 61
DataBinding with ListControls
Main Menu 47 of 61
DataBinding with ListControls
Main Menu 48 of 61
DataBinding with ListControls
Main Menu 49 of 61
DataBinding with ListControls
Main Menu 50 of 61
DataBinding with ListControls
Main Menu 51 of 61
DataBinding with ListControls
Dim DS As DataSet
Dim SqlAdapter1 As SqlDataAdapter
SqlAdapter1 = New SqlDataAdapter("fetch_authors",
SqlConnection1)
SqlAdapter1.SelectCommand.CommandType =
CommandType.StoredProcedure
SqlAdapter1.SelectCommand.Parameters.Add(New
SqlParameter("@City",
SqlDbType.VarChar, 20))
Main Menu 52 of 61
DataBinding with ListControls
Main Menu 53 of 61
DataBinding with ListControls
Main Menu 54 of 61
DataBinding with ListControls
performance.
Main Menu 55 of 61
DataBinding with ListControls
Main Menu 56 of 61
DataBinding with ListControls
Main Menu 57 of 61
DataBinding with ListControls
Summary
ListControl is the base class for most of the list controls.
Items can be added to the list at design time, run time or
through the database.
DropDownList Control is used to display one item at a time
out of the list which can is displayed on clicking the drop
button.
ListBox Control is used to display a number of items at a
time.
CheckBoxList Control is used for fetching the database
results and generates the checkboxes dynamically.
Main Menu 58 of 61
DataBinding with ListControls
Summary
RadioButtonList Control is used for fetching the database
results and generates the RadioButtons dynamically.
DataGrid control is used to display the data fetched from the
database in table like format.
DataList Control provides a GUI interface, drag and drop
feature to edit templates.
Repeater control requires the programmer to switch to the
HTML view write all the coding for the templates.
Table control is used to fetch data from the database and
display in a table. Rows and columns in a table control can be
generated dynamically.
Main Menu 59 of 61
DataBinding with ListControls
Self-Assessment
Fill in the blanks:
The DataGrid displays ___________ data by default.
To capture the information from the database, ________
object is used.
To populate the information from the database, __________
method is used.
The ________ control is a basic container control that
allows creating custom lists out of any data available to the
page.
In Repeater control, _________ view is used to edit the
templates.
In ___________ we specify the number of columns per
row.
The DataGrid can contain an _____________ that renders
links for firing three special
Mainevents.
Menu 60 of 61
DataBinding with ListControls
Self-Assessment
State True or False
By default, the DataGrid control generates a bound column
for each field in the data source.
DataBinder.Eval method has four parameters
The DataGrid directly supports sorting.
Main Menu 61 of 61