English 中文(简体)
ASP.Net Repeater Header Per Group (i.e. Month)
原标题:

I have a datasource which contains dated items per row. These will be bound to the repeater and ordered by date. I d like to present each month as a seperate table when rendered but is there a way to do this with a repeater control without having to have multiple repeaters added dynamically from the server side code?

Ideally I d like the following:

Examples Data:

Row 1: Title 1, 01/12/2009
Row 2: Title 1, 02/12/2009
Row 1: Title 1, 01/01/2010
Row 1: Title 1, 02/01/2009

Required output:

Dec 09
-------------------------------
Title  |  Date                 |
-------------------------------|
Title1 |  01/12/2009           |
-------------------------------|
Title2 |  02/12/2010           |
-------------------------------|

Jan 10
-------------------------------
Title  |  Date                 |
-------------------------------|
Title1 |  01/01/2010           |
-------------------------------|
Title2 |  02/01/2010           |
-------------------------------|
最佳回答

You can use PlaceHolder controls within the ItemTemplate to contain the repeating goup headers and footers. Then set the visibility of the PlaceHolders in the OnItemDataBound or OnItemCreated event handlers based on the dates. Like this:

<ItemTemplate>

<asp:PlaceHolder id="table_end" visible="<%# _newMonthStarting && (!_firstMonth) %>" runat="server">
  </table>
</asp:PlaceHolder>

<asp:PlaceHolder id="table_end" visible="<%# _newMonthStarting %>" runat="server">
  <div><%# _monthHeader</div>
  <table>
    <tr>
      <th>Title</th>      
      <th>Date</th>      
    </tr>
</asp:PlaceHolder>
  <tr>
    <td><!-- data --></td>
    <td><!-- data --></td>
  </tr>
</ItemTemplate>

<FooterTemplate>
   </table>
</FooterTemplate>
问题回答

Your example data and your output do not match, but I think I know what you are going for here.

I would work this out to get 1 set of data to bind into the main repeater, this would be your "Dec 09" and "Jan 10" data. Inside the item template have another repeater, defined once, and simply use the "OnDataBound" event to load data to the second repeater.

It is simple, effective and easy to manage.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签