Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Repeater HeaderTemplate

Is there a way to access the field header name of a databound repeater within the header template. So insted of this....

                      <HeaderTemplate>
                          <table >
                              <th ></th>
                              <th >Forename</th>
                              <th >Surname</th>
                              <th >work email</th>
                              <th ></th>
                      </HeaderTemplate>

     We get something like this.                 

                      <HeaderTemplate>
                          <table >
                              <th ></th>
                              <th ><%# Eval("Forename").HeaderName%></th>
                              <th ><%# Eval("SureName").HeaderName%></th>
                              <th ><%# Eval("WorkEmail").HeaderName%></th>
                              <th ></th>
                      </HeaderTemplate>
like image 251
Antony Delaney Avatar asked Sep 24 '09 08:09

Antony Delaney


1 Answers

You could move the table header into your ItemTemplate like this:

<ItemTemplate>
    <asp:Panel runat="server" Visible='<%# Container.DisplayIndex == 0 %>'>
        <tr>
            <th><%# Eval("Forename").HeaderName %></th>
        </tr>
    </asp:Panel>
    <tr>
        <td><%# Eval("Forename") %></td>
    </tr>
</ItemTemplate>

although this is slightly wasteful since the header would be bound for each row (only the first shown though). Perhaps it would be better to use <% if (...) %> instead of a Panel but I don't know how to access the Container.DisplayIndex in that context.

Edit:

In .net 4.5 Container.DisplayIndex does not work; replace with Container.ItemIndex.

Full example:

<ItemTemplate>
    <asp:Panel runat="server" Visible='<%# Container.ItemIndex == 0 %>'>
        <tr>
            <th><%# Eval("Forename").HeaderName %></th>
        </tr>
    </asp:Panel>
    <tr>
        <td><%# Eval("Forename") %></td>
    </tr>
</ItemTemplate>
like image 159
2 revs, 2 users 59% Avatar answered Oct 20 '22 23:10

2 revs, 2 users 59%