2011-03-15 80 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

你可以使用兩個嵌套中繼器,就像本文:[在ASP.NET中使用嵌套中繼器的快速指南](http://www.codeproject.com/KB/aspnet/AspNetNestedRepeaters.aspx) –

+0

hmm似乎它可以工作,我試圖只寫一個簡單的if語句,檢查e.row是否<>到e.row.index - 1列,那麼它會做一個小計是完全關閉 – 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事件處理程序(我「將不得不去爲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等,這將創建和數據瑪麗行自動。在網絡上有幾個關於這個的教程,但是在我切換到MVC之前,我找不到那個偉大的教程。

+0

嗯,我將它轉換爲vb.net並在這裏得到一個錯誤 Dim Tour = TryCast(e.Item.DataItem,Tour) tour is沒有定義 – MyHeadHurts

+0

對不起。 e.Item.DataItem在頁腳中爲空。請參閱修訂後的代碼,這也修復了小計計算中的錯誤。 – carlsb3rg

+0

是啊bt錯誤是在 Dim tour = TryCast(e.Item.DataItem,Tour) – MyHeadHurts