ASP.NET & Databases Part 4
page 2 of 5
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 22707/ 67

The Repeater Control

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 -

  1. 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).
  2. Then we bind the data to the repeater.
  3. We start the repeater in the normal way to start any Web Form Control.
  4. The HeaderTemplate starts a table with 2 columns, First Name and Last Name.
  5. The ItemTemplate fills each column with the appropriate data, 'Container refers to the DataSource, we'll talk about that later (in another Part).
  6. 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.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-02 8:33:02 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search