2012-08-08 1 views
0

foreach 문으로 ListView를 반복하려고하는데 항목의 하위 항목을 가져올 수 없습니다. For 문으로 성공하지 못했습니다. IntelliSense는 두 가지 방법으로 제안하지 않습니다. 뒤에반복하는 동안 ListView의 하위 항목에 액세스 할 수 없습니다.

코드 :

protected void btnNext_Click(object sender, EventArgs e) 
{ 
    foreach (ListViewItem item in ListView1.Items) 
    { 
     item. *(here a should get the Subitems)* 

    } 
} 

ASPX

<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1"> 
    <LayoutTemplate> 
     <table> 
     <tr> 
      <th>Customer</th> 
      <th>Item No</th> 
     </tr> 
     <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder> 
     </table> 
    </LayoutTemplate> 
    <ItemTemplate>  
      <tr>  
       <td> 
        <%# Eval("CustomerName") %> 
       </td> 
       <td> 
        <%# Eval("Item") %> 
       </td> 
      </tr> 
    </ItemTemplate> 
    </asp:ListView> 

답변

0

당신이 의지 you..happy 코딩하는 데 도움이 파일 뒤에 코드에서 다음과 같이 각 루프

string strProductNames = string.Empty; 
foreach (ListViewItem item in ListView1.Items) 
    { 
     Label lblCustomerName= (Label)item.FindControl("lblCustomerName"); 

     // strProductNames = strProductNames + lblCustomerName.Text + "<br/>"; 
     // you can get values in lblCustomerName.Text. use this value as per your requirement 
    } 

희망을 사용해야 이제

<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1"> 
<LayoutTemplate> 
    <table> 
    <tr> 
     <th>Customer</th> 
     <th>Item No</th> 
    </tr> 
    <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder> 
    </table> 
</LayoutTemplate> 
<ItemTemplate>  
     <tr>  
      <td> 
      <asp:Label ID="lblCustomerName" Text='<%# Eval("CustomerName") %>' runat="server"></asp:Label> 
      </td> 
      <td>      
      <asp:Label ID="lblItem" Text='<%# Eval("Item") %>' runat="server"></asp:Label> 
      </td> 
     </tr> 
</ItemTemplate> 
</asp:ListView> 

아래로 aspx 페이지를 변경해야

0
당신이 listview.items 통해 서 루프를 사용한다


for (int j = 0; j < this.listView1.Items.Count; j++) 
      { 
       ListViewItem item = 
        (ListViewItem)this.listView1.ItemContainerGenerator.ContainerFromIndex(j); 

      }