views:

176

answers:

2

I have an ASP ListView which I am using to display some pivoted information (across years) in a table. Currently I have the ListView templates defined as:

<LayoutTemplate>
  <table id="listViewTable" class="tableData">
    <tr class="rowHeader">
        <td style="text-align: left;">Rank / Equivalent Rank</td>
        <td>2004</td>
        <td>2005</td>
        <td>2006</td>
        <td>2007</td>
        <td>2008</td>
        <td>2009</td>
        <td>2010</td>
    </tr>
    <tr id="itemPlaceholder" runat="server"></tr>
  </table>
</LayoutTemplate>
<ItemTemplate>
  <%#addListViewSectionHeaderRow()%>
    <%#addListViewRankValueCell()%>
    <td class="cellNumeric"><%#formatNumber(Eval("2004"))%></td>
    <td class="cellNumeric"><%#formatNumber(Eval("2005"))%></td>
    <td class="cellNumeric"><%#formatNumber(Eval("2006"))%></td>
    <td class="cellNumeric"><%#formatNumber(Eval("2007"))%></td>
    <td class="cellNumeric"><%#formatNumber(Eval("2008"))%></td>
    <td class="cellNumeric"><%#formatNumber(Eval("2009"))%></td>
    <td class="cellNumeric"><%#formatNumber(Eval("2010"))%></td>
  </tr>
</ItemTemplate>

As you can see in the Eval() statements, the columns in the query behind the scenes already selects the years as they are displayed on the page.

How do I just display the column names from the query in the LayoutTemplate instead of the hard-coded values I have? I thought this would be easy to find in documentation somewhere, but I can't find anything...but maybe I'm not looking in the right place...

Thanks!


Edit: I guess I also have a problem with defining the actual years in the Eval() statements. I want to get this as automatic as possible so when, for example, 2011 data appears, the page reflects only 2005-2011 years. Maybe I'm doing something fundamentally wrong with that goal in mind?

A: 

If you're not dead-set on using a ListView, you could instead use a GridView setting the AutoGenerateColumns property to true. This way your results table will automatically pick up all the available columns from your results set.

mdresser
+1  A: 

ListView or GridView, this should be pretty easy. As long as you always want to base this on the current year, you can do something like this:

<asp:ListView ID="ListView1" runat="server">
        <LayoutTemplate> 
            <table id="listViewTable" class="tableData"> 
            <tr class="rowHeader"> 
                <td style="text-align: left;">Rank / Equivalent Rank</td> 
                <td><%= DateTime.Today.AddYears(-6).Year%></td> 
                <td><%= DateTime.Today.AddYears(-5).Year%></td> 
                <td><%= DateTime.Today.AddYears(-4).Year%></td> 
                <td><%= DateTime.Today.AddYears(-3).Year%></td> 
                <td><%= DateTime.Today.AddYears(-2).Year%></td> 
                <td><%= DateTime.Today.AddYears(-1).Year %></td> 
                <td><%= DateTime.Today.Year %></td> 
            </tr> 
            <tr id="itemPlaceholder" runat="server"></tr> 
            </table> 
        </LayoutTemplate> 
        <ItemTemplate> 
            <tr>
                <%#addListViewSectionHeaderRow()%> 
                <%#addListViewRankValueCell()%> 
                <td class="cellNumeric"><%#formatNumber(Eval(DateTime.Today.AddYears(-6).Year.ToString()))%></td> 
                <td class="cellNumeric"><%#formatNumber(Eval(DateTime.Today.AddYears(-5).Year.ToString()))%></td> 
                <td class="cellNumeric"><%#formatNumber(Eval(DateTime.Today.AddYears(-4).Year.ToString()))%></td> 
                <td class="cellNumeric"><%#formatNumber(Eval(DateTime.Today.AddYears(-3).Year.ToString()))%></td> 
                <td class="cellNumeric"><%#formatNumber(Eval(DateTime.Today.AddYears(-2).Year.ToString()))%></td> 
                <td class="cellNumeric"><%#formatNumber(Eval(DateTime.Today.AddYears(-1).Year.ToString()))%></td> 
                <td class="cellNumeric"><%#formatNumber(Eval(DateTime.Today.Year.ToString()))%></td> 
            </tr> 
        </ItemTemplate>
    </asp:ListView>

Really, all I'm doing is telling the form to use current year minus X. That way, the years change automatically based on when the form is viewed.

I wasn't able to test this, so forgive my typo's, if any.

Byron Sommardahl
Haha...man, didn't even think of just using a programmatic date! I'll probably go in this direction and use the data to determine the latest valid year of reporting data and go from there, rather than just current year. This solves both of my problems, thanks for switching on that light bulb :)
chucknelson