AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Friday 15 January 2016

How to use a Repeater control in ASP.NET with an example.

Repeater Control is a lightweight control it loads very quickly on the webpage. It is used to display the repeated list of items. I used this control most of the time because it is easy to use & lightweight. Let's discuss how to use the Repeater control?

Structure of Repeater Control
Repeater control is quite similar to List View. Here is the structure of Repeater Control,
<asp:Repeater ID="Rpt" runat="server">
 <HeaderTemplate>
    <%-- Header Area--%>
 </HeaderTemplate>
 <ItemTemplate>
  <%-- Create Item Template Here--%>
 </ItemTemplate>
 <FooterTemplate>
    <%-- Footer Area--%>
 </FooterTemplate>
</asp:Repeater>
  • Header Template: This section is used to write heading. Its content will not repeat & show one time.
  • Item Template: This section will create a body. Its content will bind from Data source. 
  • Footer Template: This section will create footer part & it will appears one time.
Example of Repeater Control
I will show data in table using Repeater control.

ASPX Page Code

 <asp:Repeater ID="RptAgencies" OnItemDataBound="RptAgencies_ItemDataBound" OnItemCommand="RptAgencies_ItemCommand" runat="server">
 <HeaderTemplate>
    <%-- Header Area--%>
 </HeaderTemplate>
 <ItemTemplate>
  <tr class="odd gradeX">
   <td><%#Eval("AgencyID").ToString() %></td>
   <td><%#Eval("AgencyName").ToString() %></td>
   <td><a href="<%#Eval("WebSiteURL").ToString() %>" target="_blank"><%#Eval("WebSiteURL").ToString() %></a></td>

   <td class="center">                                                
    <asp:LinkButton ID="LnkDelete" CssClass="btn btn-danger" Text="Delete" runat="server"></asp:LinkButton>                                                
   </td>

  </tr>
 </ItemTemplate>
 <FooterTemplate>
    <%-- Footer Area--%>
 </FooterTemplate>
</asp:Repeater>
.
.CS PageCode

    #region ---Bind Agencies---
    protected void BindAgencies()
    {
        DataTable AgencyData= GetAgencyData(); //Write your logic to Get Data
        RptAgencies.DataSource = AgencyData;
        RptAgencies.DataBind();
    }
   
    protected void RptAgencies_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRow data = (DataRow)e.Item.DataItem;
            LinkButton LnkDelete = (LinkButton)e.Item.FindControl("LnkDelete");
            if (LnkDelete != null)
            {
                LnkDelete.CommandName = "Delete";
                LnkDelete.CommandArgument = data["AgencyId"].ToString();
            }

        }
    }
    protected void RptAgencies_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
           DeleteAgency(int.Parse(e.CommandArgument.ToString())); //Write your logic to Delete Data
        }
    }
    #endregion
Data will appears as shown in figure.

   1 comment :

  1. It's amazing. The best site I have ever found for self learning.

    ReplyDelete

  • Popular Posts
  • Comments