Repeater data binding : Repeater « Asp Control « ASP.Net






Repeater data binding


<script language="C#" runat="server">
public class State {
    string _name;
    string _timezone;
    public State(string name, string timezone) {
        _name = name;
        _timezone = timezone;
    }
    public string Name {
        get { return _name; }
    }
    public string TimeZone {
        get { return _timezone; }
    }
}
protected void Page_Load(object o, EventArgs e) {
    if(!IsPostBack) {
        ArrayList states = new ArrayList();
        states.Add(new State("Washington", "Pacific"));
        states.Add(new State("Utah", "Mountain"));

        repeaterVertical.DataSource = states;
        repeaterHorizontal.DataSource = states;

        repeaterVertical.DataBind();
        repeaterHorizontal.DataBind();
    }
}
</script>
<form runat="server">
    <asp:Repeater runat="server" id="repeaterVertical">
        <HeaderTemplate>
           <table><tr><th>State</th><th>TimeZone</th></tr>
        </HeaderTemplate>
        <ItemTemplate>
           <tr>
              <td><%#((State)(Container.DataItem)).Name %></td>
              <td><%#DataBinder.Eval(Container, "DataItem.TimeZone") %></td>
           </tr>
        </ItemTemplate>
        <SeparatorTemplate><tr><td><hr></td></tr></SeparatorTemplate>
        <AlternatingItemTemplate>
           <tr>
              <td><%#((State)(Container.DataItem)).Name %></td>
              <td><%#DataBinder.Eval(Container, "DataItem.TimeZone") %></td>
           </tr>
        </AlternatingItemTemplate>
        <FooterTemplate></table></FooterTemplate>
    </asp:Repeater>
   <asp:Repeater runat="server" id="repeaterHorizontal">
       <HeaderTemplate><table><tr><th>State<br/>TimeZone</th>
       </HeaderTemplate>
       <ItemTemplate>
          <td>
            <%#DataBinder.Eval(Container.DataItem, "Name") %><br />
            <%#DataBinder.Eval(Container.DataItem, "TimeZone") %>
          </td>
       </ItemTemplate>
       <AlternatingItemTemplate>
          <td>
            <%#DataBinder.Eval(Container.DataItem, "Name") %><br />
            <%#DataBinder.Eval(Container.DataItem, "TimeZone") %>
          </td>
       </AlternatingItemTemplate>
       <FooterTemplate></tr></table></FooterTemplate>
</asp:Repeater>
</form>

 








Related examples in the same category

1.ItemTemplate in a repeater
2.SeparatorTemplate in a repeater
3.SelectedItemTemplate in repeater