Posts

Showing posts with the label databinding

Binding data to a Repeater using Lazy Loading (ASP.NET C#)

Lazy loading is a method of only loading data as and when you need it. Rather than loading it on page load, you can define a property that can then be bound to a WebControl. For example, in the page is an asp:Repeater : <asp:Repeater ID="MyRepeater" DataSource='<%# MyData %>' runat="server"> <HeaderTemplate><ul></HeaderTemplate> <ItemTemplate><li><a href="<%# Eval("Url") %>"><%# Eval("Text") %></a> (Record ID: <%# Eval("RecordID") %>)</li></ItemTemplate> <FooterTemplate></ul></FooterTemplate> </asp:Repeater> MyData in the DataSource attribute of asp:Repeater is a property defined in the CodeBehind page: private DataTable _MyData; public DataTable MyData { get { if (_MyData == null) { _...

DataBinding to an Enumeration / enum (C#, ASP.NET)

Enumeration types in C# can be used for many purposes. One such use is to reduce repetitive typing as they can be bound to server controls (e.g. Repeaters). In code behind: public enum WeekDays { Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday } In your page: <asp:Repeater runat="server" DataSource='<%# Enum.GetValues(typeof(WeekDays)) %>'> <HeaderTemplate> <table> <thead> <tr> <th>Day</th> <th>Start Time</th> <th>End Time</th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td><%# Container.DataItem %></td> <td><%# GetStartTime(Container.DataItem) %></td> <td><%# GetEndTime(Container.DataItem) %></td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater> GetStartT...