Bind Data to a Repeater Control
Example of binding a data source to a repeater control in ASP.Net.
<!-- define the repeater control --> <asp:Repeater ID="repTimeOffStatus" runat="server"> <!-- add header text here. This will appear at the start of the repeater. --> <HeaderTemplate> <table class="table table-hover" style="margin-bottom: 0;"> <thead> <tr> <th></th> <th class="text-center">Sick</th> <th class="text-center">Vacation</th> <th class="text-center">Rollover</th> <th></th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <!-- Add the block to be repeated in here. This block will be repeated for each element / row in the datasource. Use the Eval() method to evaluate the current row in the datasource and output the value to the web page. --> <tr> <td class="text-right"><%# Eval("RowType") %></td> <td class="text-center"><%# Eval("Sick", "{0:0.###}") %></td> <td class="text-center"><%# Eval("Vacation", "{0:0.###}") %></td> <td class="text-center"><%# Eval("Rollover", "{0:0.###}") %></td> <td><%# Eval("RowType") %></td> </tr> </ItemTemplate> <!-- add the footer text here. This will appear at the end of the repeater. --> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater>
The code behind. This is called during the Page_Load() of the form.
// create our BL class that does the work of getting the data using (var bl = new code.BL.blTimeOffStatus()) // use the BL object to get the data using (DataTable tbl = bl.GetTimeOffStatus()) { // set the datasource on the repeater control this.repTimeOffStatus.DataSource = tbl; // Call DataBind on the repeater. // But, we only want to do this if we are NOT in a PostBack or Callback. if (!this.Page.IsPostBack && !this.Page.IsCallback) { this.repTimeOffStatus.DataBind(); } }