Repeater
I'll tell you now that all of these methods of
viewing data is tedious and very complicated, but I'm sure that you can work
it out, your relatively smart humans.
The Repeater is a control that loops through
the data and displays it on templates that you create.
- ItemTemplate. This is the template
where it displays the main body of the data.
- AlternativeItemTemplate. You can have
an alternating style for every second item if you want.
- HeaderTemplate. For any header you
want.
- SeparatorTemplate. For separating
items (usually in the form of <br>, <hr> etc.).
- FooterTemplate. For footers!
Before I show you the code there is one thing
that I should tell you about.
DataBind()
DataBind() is a method that takes all of the
data on the page and binds it to the controls that you have specified for
example.
Repeater1.DataSource =
ds.Tables("users").DefaultView
DataBind() |
The first line sets the datasource and the
second binds the data to the repeater, without binding, the data wouldn't be
shown.
Repeater cont.
Remember that this code just snaps on to the
one in the introduction.
Rpt.DataSource =
ds.Tables("users").DefaultView
DataBind()
End Sub
</script><html><body><font
face="Arial" size="2">
<asp:repeater id="Rpt" runat="server">
<HeaderTemplate>
<table border="0" cellspacing="1" cellpadding="3">
<tr>
<td bgcolor="#6699FF" width="25%">Last Name</td>
<td bgcolor="#6699FF" width="25%">First Name</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Container.DataItem("LName") %>
</td><td>
<%# Container.DataItem("FName") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
</font></body></html> |
Lets look at this in sections -
- We first set the repeater's datasource,
which is the datatable "users". DefaultView is the view of the table, we'll
look at views later (in another Part).
- Then we bind the data to the repeater.
- We start the repeater in the normal way to
start any Web Form Control.
- The HeaderTemplate starts a table
with 2 columns, First Name and Last Name.
- The ItemTemplate fills each column
with the appropriate data, 'Container refers to the DataSource, we'll talk
about that later (in another Part).
- Finally the FooterTemplate closes the
table and we then close up the remaining open tags.
I haven't put in any kind of Alternating Item
in here, but its simple enough.
That is all a Repeater can do, it presents data
and that is all there is to it.