Saturday, April 5, 2014

ASP.Net Repeater

Rendering a simple repeater

As this is a sample app, prepare a data source. In this example I have come up with a basic DataTable.

            DataTable table = new DataTable("SelectionTable");
            table.Columns.Add(new DataColumn("Name", typeof(string)));
            table.Columns.Add(new DataColumn("Email", typeof(string)));
            table.Columns.Add(new DataColumn("Address", typeof(string)));
            table.Columns.Add(new DataColumn("Phone", typeof(string)));


1)  Add a repeater in the aspx page

The tag in the aspx page when the repaeter is added will be as :

<asp:Repeater ID="EmployeesRepeater" runat="server">

2) Set the Data source to the repeater. 

Here, I set it at the page load method in the code behind page.

            if (table != null)
            {
                //Sample data
                table.Rows.Add("Shaamil", "shaamil@abc.com", "111 aaa", "0111");
                table.Rows.Add("Joe", "joe@abc.com", "222 bbb", "0222");
                table.Rows.Add("Ramesh", "samesh@abc.com", "333 ccc", "0333");
                table.Rows.Add("Rushdi", "rushdi@abc.com", "444 ddd", "0444");
                table.Rows.Add("Sam", "sam@abc.com", "555 eee", "0555");

                //Bind data source
                EmployeesRepeater.DataSource = table;
                EmployeesRepeater.DataBind();
            }

3) Formatting the repeater rendering

<asp:Repeater ID="EmployeesRepeaterrunat="server">
   <HeaderTemplate>
     <ul>
  </HeaderTemplate>
  <ItemTemplate>
    <li>
      <span>
          <%# DataBinder.Eval(Container.DataItem, "Name") %>  &nbsp;
          <%# DataBinder.Eval(Container.DataItem, "Address") %>
      </span>
    </li>
  </ItemTemplate>
  <FooterTemplate>
   </ul>
  </FooterTemplate>
</asp:Repeater>


Result



No comments:

Post a Comment