Wednesday, December 03, 2008

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

In your page:

<asp:Repeater runat="server" DataSource='<%# Enum.GetValues(typeof(WeekDays)) %>'>
   <th>Start Time</th>
   <th>End Time</th>
   <td><%# Container.DataItem %></td>
   <td><%# GetStartTime(Container.DataItem) %></td>
   <td><%# GetEndTime(Container.DataItem) %></td>

GetStartTime and GetEndTime are just examples (so would just need writing in the code behind).

protected string GetStartTime(object day)
protected string GetEndTime(object day)

1 comment:

Sara C said...

thanks for this!