2011-03-15 11 views
0

リピータを使用してカスタムテーブルを作成しています。しかし、次の行のツアーが前の行と一致しない場合は、表の小計を表示する方法を理解できません。ネストされたリピータは、親リピータに一致するデータを表示します

同様のものです。

row1 tour1 
row2 tour 1 
tour1 subtotal 
row3 tour2 
row4 tour2 
subtotal 
total 

<asp:Repeater ID="ParentRepeater" runat="server" DataSourceID="SqlDataSource1"> 
    <HeaderTemplate> 
     <table border="1"> 
      <tr> 
       <th>TOUR</th> 
       <th>THEME</th> 
       <th>ROUTE</th> 
       <th>DEPT</th> 
      </tr> 
    </HeaderTemplate> 
    <ItemTemplate> 
     <tr> 
      <td><%#Container.DataItem("tour")%></td> 
      <td align="center"><%#Container.DataItem("theme")%></td> 
      <td align="right"><%#Container.DataItem("route")%></td> 
      <td align="right"><%#Container.DataItem("dep7")%></td> 
      <asp:Repeater ID="ChildRepeater" runat="server" 
       DataSourceID="SqlDataSource2"> 
       <HeaderTemplate> 
        <table border="1"> 
         <tr> 
          <th>BOOKNO</th> 
          <th>PARTY</th> 
          <th>TOUR</th> 
          <th>THEME</th> 
          <th>ROUTE</th> 
          <th>DEPT</th> 
          <th>HOME</th> 
          <th>USERID</th> 
         </tr> 
       </HeaderTemplate> 
       <ItemTemplate> 
        <tr> 
         <td align="center"><%#Container.DataItem("bookno") %></td> 
         <td><%#Container.DataItem("party")%></td> 
         <td><%#Container.DataItem("tour")%></td> 
         <td align="center"><%#Container.DataItem("theme")%></td> 
         <td align="right"><%#Container.DataItem("route")%></td> 
         <td align="right"><%#Container.DataItem("dep7")%></td> 
         <td align="right"><%#Container.DataItem("home")%></td> 
         <td align="right"><%#Container.DataItem("userid")%></td> 
        </tr> 
       </ItemTemplate> 
       <FooterTemplate> 
        </table> 
       </FooterTemplate> 
      </asp:Repeater> 
     </tr> 
    </ItemTemplate> 
    <FooterTemplate> 
     </table> 
    </FooterTemplate> 
</asp:Repeater> 

コード

Protected Sub ItemBound(ByVal sender As Object, ByVal args As RepeaterItemEventArgs) 
     If args.Item.ItemType = ListItemType.Item Then 
      Dim childRepeater As Repeater = DirectCast(args.Item.FindControl("ChildRepeater"), Repeater) 
      childRepeater.DataSource = SqlDataSource2 
      childRepeater.DataBind() 
     End If 
    End Sub 

後ろが、これは、ネストされたリピータ内のすべてのデータの例ツアー、テーマのparentrepeaterフィールド と一致していないものを示し、dep7は、子リピータに一致する必要があります

+0

この記事のように、ネストされたリピータを2つ使用することができます。[ネストされたリピータをASP.NETで使用するための簡単なガイド](http://www.codeproject.com/KB/aspnet/AspNetNestedRepeaters.aspx) –

+0

e.rowがe.row.index - 1カラムにあったかどうかをチェックして、単純なif文を書くことを試みていましたが、小計を行うのは – MyHeadHurts

+0

です小計を動的に含む行のHTMLを生成する必要があります。私はそれも動作するかもしれないと思うが、私は自動HTML生成がネストされたリピーター(少なくともすべてのHTMLをページ内にまとめたもの)よりも面倒だと思う。 –

答えて

0

まず、追加2クラスフィールド:

private int _subTotal; // Not sure what you're summing but you 
         // wrote subtotal in your pseudo-output 
private int _lastTourId; 
_subTotal = 0; 
_lastTourId = -1; 
TourRepeater.DataBind(); 

単一レベルのリピータ作成:

<asp:Repeater ID="TourRepeater" ...> 
    <HeaderTemplate>...<> 
    <ItemTemplate> 
    <tr> 
     Some columns with your tour data 
    </tr> 
    <PlaceHolder ID="SubTotalRow" ... /> 
    </ItemTemplate> 
    <FooterTemplate> 
    <PlaceHolder ID="SubTotalRow" ... /> 
    </FooterTemplate> 
</asp:Repeater> 

バインドあなたの旅行データに直接このRepeaterを、そしてあなたのOnItemDataBoundイベントハンドラでこれを行う(Iあなたがリピーターをデータバインドする前にこれらのフィールドをリセット「)C#のために行かなければならないでしょう:

void RepeaterItemDataBound(object source, RepeaterItemEventArgs e) 
{ 
    if (
    e.Item.ItemType == ListItemType.Item || 
    e.Item.ItemType == ListItemType.AlternatingItem // think you forgot this 
) 
    { 
    var tour = e.Item.DataItem as Tour; 
    if (tour != null) 
    { 
     if (_lastTourId == -1) { _lastTourId == tour.TourId; } // To avoid subtotal row before first tour 
     if (tour.TourId != _lastTourId) // This is a new tour so insert a subtotal row for the previous tour 
     { 
     var subTotalRow = e.Item.FindControl("SubTotalRow") as PlaceHolder; 
     if (subTotalRow != null) 
     { 
      RenderSummaryRow(subTotalRow, _subTotal); 
     } 
     _subTotal = tour.SomeValueYouWantToAddToSubTotal; 
     } 
     else 
     { 
     _subTotal += tour.SomValueYouWantToAddToSubTotal; 
     } 
     _lastTourId = tour.TourId; 
    } 
    } 
    else if (e.Item.ItemType == ListItemType.Footer) 
    { 
    var subTotalRow = e.Item.FindControl("SummaryRow") as PlaceHolder; 
    if (summaryTotalRow != null) 
    { 
     RenderSummaryRow(subTotalRow, _subTotal); 
    } 
    } 
} 

private void RenderSummaryRow(PlaceHolder placeHolder, int subTotal) 
{ 
      // create subtotal row manually by either creating TableRow + TableCells and adding them to a placeholder, or 
      // just add a literal and create your markup manually as innerHtml. Put your _subtotal value in where you want 
} 

別の方法は、DataTableのにあなたのツアーデータを取得し、合計を作成しますROLLUPなどWITHデータを取得することです自動的に行を追加します。 Web上でこれについていくつかのチュートリアルがありますが、私がMVCに切り替える前に私が使った素晴らしいものを見つけることができませんでした。

+0

hmmm vb.netに変換してここにエラーがあります Dim Tour = TryCast(e.Item.DataItem、Tour) ツアーは定義されていません – MyHeadHurts

+0

申し訳ありません。フッターのe.Item.DataItemはnullです。小計計算のバグも修正された改訂コードを参照してください。 – carlsb3rg

+0

yeah btエラーは にあります。Dim tour = TryCast(e.Item.DataItem、Tour) – MyHeadHurts

関連する問題