Tuesday 14 May 2013

HOW TO USE REPEATER


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:
  1. <asp:Repeater ID="rptList" runat="server" OnItemDataBound="rptList_ItemDataBound">  
  2. <HeaderTemplate>  
  3. <table>  
  4. <tr>  
  5. <th>  
  6. <asp:Label ID="Label14" Text="Login" runat="server"></asp:Label>  
  7. </th>  
  8. <th>  
  9. <asp:Label ID="Label1" Text="First Name" runat="server"></asp:Label>  
  10. </th>  
  11. <th>  
  12. <asp:Label ID="Label2" Text="Last Name" runat="server"></asp:Label>  
  13. </th>  
  14. <th>  
  15. <asp:Label ID="Label3" Text="Date Added" runat="server"></asp:Label>  
  16. </th>  
  17. <th>  
  18. <asp:Label ID="Label6" Text="Delete" runat="server"></asp:Label>  
  19. </th>  
  20. </tr>  
  21. </HeaderTemplate>  
  22. <ItemTemplate>  
  23. <tr>  
  24. <td>  
  25. <asp:LinkButton ID="lbnLogin" Text='<<%# Eval("Login"%>runat="server"  
  26. OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>  
  27. </asp:LinkButton>  
  28. </td>  
  29. <td>  
  30. <asp:LinkButton ID="lbnFirstName" Text='<<%# Eval("FirstName").ToString() %>'  
  31. runat="server"  
  32. OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>  
  33. </asp:LinkButton>  
  34. </td>  
  35. <td>  
  36. <asp:LinkButton ID="LinkButton2" Text='<<%# Eval("LastName").ToString() %>'  
  37. runat="server"  
  38. OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>  
  39. </asp:LinkButton>  
  40. </td>  
  41. <td>  
  42. <asp:LinkButton ID="LinkButton1" Text='<<%# Eval("DateAdded"%>runat="server"  
  43. OnClick="lnkClient_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>  
  44. </asp:LinkButton>  
  45. </td>  
  46. <td>  
  47. <asp:LinkButton ID="lbnDelete" Text="delete" runat="server"  
  48. OnClick="lbnDelete_Click" CommandArgument='<<%# Eval("ID").ToString() %>'>  
  49. </asp:LinkButton>  
  50. </td>  
  51. </tr>  
  52. </ItemTemplate>  
  53. <FooterTemplate>  
  54. </table>  
  55. </FooterTemplate>  
  56. </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.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. SqlConnection con = new SqlConnection("Server=xxxxxxx;UID=xxxx;PWD=xxxxx;Database=NetroStar\Server");  
  4. string sSQL = "Select * from Client_NetroStar";  
  5. SqlCommand cmd = new SqlCommand(sSQL, con);  
  6. con.Open();  
  7. SqlDataReader dtrClient = cmd.ExecuteReader();  
  8. rptList.DataSource = dtrClient;  
  9. rptList.DataBind();  
  10. con.Close();  
  11. }  
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