【问题标题】:Can't access Subitems in ListView while looping循环时无法访问 ListView 中的子项
【发布时间】:2012-08-08 11:54:30
【问题描述】:

我正在尝试使用 foreach 语句遍历 ListView,但我似乎无法获取项目的子项。 For 语句也没有成功。 IntelliSense 不会在这两种方式上都建议它。

代码背后:

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>

【问题讨论】:

    标签: asp.net listview listviewitem subitem


    【解决方案1】:

    你必须改变你的 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>
                 <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>
    

    现在您必须在文件后面的代码中使用每个循环,如下所示

    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
        }
    

    希望这会帮助你..快乐编码

    【讨论】:

      【解决方案2】:

      你应该使用循环遍历 listview.items

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

      【讨论】:

        【解决方案3】:

        Get data being bound to ListView on DataBound event

        您可以像在 listView1_ItemDataBound 事件中处理代码一样对待循环内的代码。

        【讨论】:

          猜你喜欢
          • 2014-01-22
          • 2014-11-04
          • 2020-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-22
          • 2010-11-27
          • 1970-01-01
          相关资源
          最近更新 更多