HOW TO USE REPEATER
N THIS ARTICLE, I'LL SHOW YOU HOW TO USE REPEATER IN ASP.NET PAGE.
Here is an example of Repeater:
I store the user in table "Client_NetroStar" in SQL Server. There are five fields in the table:
- ID
- Login
- FirstName
- LastName
- DateAdded
1. FIRST STEP - INSERT THE REPEATER CONTROL ON PAGE.
Repeater control allows you to create templates. For example you can use a template to customize the layout of the individual rows.
- HeaderTemplate - template for elements that you want to render once before your ItemTemplate section.
- ItemTemplate - template for elements that are rendered once per row of data.
- AlternatingItemTemplate - template for elements that are rendered every other row of data. This allows you to alternate background colors, for example.
- SeparatorTemplate - template for elements to render between each row, such as line breaks.
- FooterTemplate - template for elements that you want to render once after your ItemTemplate section.
Here is the part of the “ClientNetroStar.aspx” page that contains Repeater:
- <asp:Repeater ID="rptList" runat="server" OnItemDataBound="rptList_ItemDataBound">
- <HeaderTemplate>
- <table>
- <tr>
- <th>
- <asp:Label ID="Label14" Text="Login" runat="server"></asp:Label>
- </th>
- <th>
- <asp:Label ID="Label1" Text="First Name" runat="server"></asp:Label>
- </th>
- <th>
- <asp:Label ID="Label2" Text="Last Name" runat="server"></asp:Label>
- </th>
- <th>
- <asp:Label ID="Label3" Text="Date Added" runat="server"></asp:Label>
- </th>
- <th>
- <asp:Label ID="Label6" Text="Delete" runat="server"></asp:Label>
- </th>
- </tr>
- </HeaderTemplate>
- <ItemTemplate>
- <tr>
- <td>
- <asp:LinkButton ID="lbnLogin" Text='<<%# Eval("Login") %>' runat="server"
- OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>
- </asp:LinkButton>
- </td>
- <td>
- <asp:LinkButton ID="lbnFirstName" Text='<<%# Eval("FirstName").ToString() %>'
- runat="server"
- OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>
- </asp:LinkButton>
- </td>
- <td>
- <asp:LinkButton ID="LinkButton2" Text='<<%# Eval("LastName").ToString() %>'
- runat="server"
- OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>
- </asp:LinkButton>
- </td>
- <td>
- <asp:LinkButton ID="LinkButton1" Text='<<%# Eval("DateAdded") %>' runat="server"
- OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>
- </asp:LinkButton>
- </td>
- <td>
- <asp:LinkButton ID="lbnDelete" Text="delete" runat="server"
- OnClick="lbnDelete_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>
- </asp:LinkButton>
- </td>
- </tr>
- </ItemTemplate>
- <FooterTemplate>
- </table>
- </FooterTemplate>
- </asp:Repeater>
The Repeater has an attribute ID= "rptList". It is used in Code Behind (ClientNetroStar.aspx.cs) . Repeater uses the HeaderTemplate to insert <table> . Then it uses the ItemTemplate to display a LinkButton control that has our data in it. I'll come back to this in the next step.
The FooterTemplate is not necessary, but I put it in </table>.
2. SECOND STEP - GET THE DATA TO REPEATER.
Here is the Page_Load event in the Code Behind file.
- protected void Page_Load(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection("Server=xxxxxxx;UID=xxxx;PWD=xxxxx;Database=NetroStar\Server");
- string sSQL = "Select * from Client_NetroStar";
- SqlCommand cmd = new SqlCommand(sSQL, con);
- con.Open();
- SqlDataReader dtrClient = cmd.ExecuteReader();
- rptList.DataSource = dtrClient;
- rptList.DataBind();
- con.Close();
- }
The first five lines open a database connection and retrieve the contents of the Client_NetroStar table. The last two lines bind our Repeater control to the DataReader.
No comments:
Post a Comment