Chapter 4 (Part 3)
Chapter 4 (Part 3)
Chapter 4 (Part 3)
What Are You Going To Learn?
• At the end of this lesson, you will be able to:
– use data bound controls
– apply data binding to data bound controls, such as
DropDownList
2/1
Data Bound Controls and Data Source Controls
Namespace: System.Web.UI.WebControls
4/1
SqlDataSource Control
The "Sql" in the control name does not refer to
Microsoft SQL Server, but rather the SQL syntax for
querying relational databases.
5/1
Data Bound Control
• render data as markup to the requesting
browser.
• can bind to a data source control and
automatically fetch data at the appropriate
time in the page request lifecycle.
6/1
Data Bound Control (cont..)
• ASP.NET includes the data-bound controls
described as following:-
– GridView
– DetailsView
– FormView
– DataList
– Repeater
– List controls
• BulletedList, CheckBoxList, DropDownList, ListBox, and
RadioButtonList controls.
7/1
Data Bound Control (cont..)
– Menu
• Renders data in a hierarchical dynamic menu that can
include submenus.
– AdRotator
• Renders advertisements on a page as an image that
users can click to go to a URL associated with the
advertisement.
– TreeView
• Renders data in a hierarchical tree of expandable
nodes.
8/1
Overview of Data Binding
• Data Binding:
– Process of dynamically assigning a value to a
property of a control at run time.
• Example:
– You can use data binding to bind the properties of
a control to a data source such as the content of a
Microsoft SQL Server Express table.
• Data binding enables you to control exactly
when a value is bound to a property.
9/1
Binding a Server Control to a Data Source
10/1
Gridview
Control displays data as a table and provides
the capability to sort columns, page through
data, and edit or delete a single record.
11/1
GridView
• You can customize the appearance and behavior of the
GridView control by doing the following:
w
DetailsVie
Gridvie
w
15/1
DetailsView
DetailsView offers
additional
customization through
templates, which give
you more control over
the rendering of certain
elements
GridView Table Read and edit Separate column for each field
Each field value in its own cell
Display multiple records in a grid
Edit existing records
DetailViews Table or Tree Read, edit, create Display single records
Default structure provided
Edit existing records
Create new records
FormView Table or Tree Read, edit, create Display single records
No default structure
Edit existing records
Create new records 22/1
Question
• Suggest the appropriate data bound control
that can be used to create the following
effects:
(a) (b)
23/1
DataList
DataList control
explicitly places
items in an
HTML table
26/1
Templates
Types Description
HeaderTemplate Contains the text and controls to render at the beginning and
/FooterTemplate end o the list, respectively.
ItemTemplate Contains the HTML elements and controls to render once for
each data item in the data source.
AlternatingItemTemplate Contains the HTML elements and controls to render once for
every other data item in the data source. Typically, this
template is used to create a different look for the alternating
items, such as a different background color than the color that
is specified in the ItemTemplate.
SeparatorTemplate Contains the elements to render between each item. A typical
example might be a line (using an <hr> element).
27/1
DEMO
(b)
(c)
(d)
29/1
Repeater
Repeater control has no
default look (while DataList
places items in a table)
30/1
Repeater
• Because the Repeater control has no default look, you can
use it to create many kinds of lists, including the following:
31/1
Using Repeater Template
• Similar to DataList, all fields are displayed in
one cell on a repeater (one cell = one record)
• You must use templates to control output
formatting.
32/1
Repeater
<asp:Repeater ID="Rpt1" runat="server" DataSourceID = "DataSource1" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "FirstName" ) %><br/>
</ItemTemplate>
</asp:Repeater>
Container:
• The object reference against which the expression is
evaluated. This must be a valid object identifier in the
page's specified language.
expression
• The navigation path from the container to the
property value to be placed in the bound control
property.
• This must be a string type of property or field names
34/1
Repeater
<asp:Repeater ID="Rpt1" runat="server" DataSourceID = "DataSource1" >
<ItemTemplate>
<%#Eval("LastName" )%>, <%#Eval("FirstName“)%> <br/>
</ItemTemplate>
</asp:Repeater>
CheckBoxList1.DataSource = dtrEmployees;
CheckBoxList1.DataTextField = "lastName";
//Use DataValueField if Value != Text
CheckBoxList1.DataValueField = " EmployeeID";
CheckBoxList1.DataBind();
…
38/1
Question
• Assuming part of the code is shown below. Discuss the
problem if the page is loaded again (for the 2nd time).
void Page_Load(object sender, eventArgs e)
{
string strCon = ConfigurationManager.ConfigurationSt…….;
SqlConnection con = new SqlConnection(strCon);
…
dtrStates = cmdSelectState.ExecuteReader();
ddlState.DataSource = dtrStates;
ddlState.DataTextField = “state";
ddlState.DataBind();
con.Close();
dtrStates.Close();
} 39/1
Binding to the List Controls – with Code
• What happen if neglecting the IsPostBack property:
– Decrease the performance of the page because the
records need to be retrieved from the database
every time the page is opened. Retrieving records
from a database is a resource-intensive task.
– if a user selects an Employees from the
DropDownList control, and you rebind the
DropDownList to the database table, the user’s
selection is lost. Binding a DropDownList resets the
SelectedIndex and SelectedItem properties of the
control. 40/1
Data-bound Controls Compariso
DataList Table or Tree Read and edit All field in one cell
One cell equals one record
Display multiple records in a grid
Create new record for GridView
Repeater Table or Tree Read only All field in one cell
One cell equal one record
Display multiple records in a grid.
Create new record for GridView display
41/1
®
Next Week