无法访问子项中的ListView而循环 [英] Can't access Subitems in ListView while looping

查看:142
本文介绍了无法访问子项中的ListView而循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个foreach语句一个ListView,但我似乎无法得到项目的子项。有没有成功语句中。
IntelliSense不建议它是双向的。

code背后:

 保护无效btnNext_Click(对象发件人,EventArgs的发送)
{
    的foreach(在ListView1.Items ListViewItem的项目)
    {
       项目。 *(这里应该得到的子项)*    }
}

ASPX

 < ASP:ListView控件ID =ListView1的=服务器的DataSourceID =ObjectDataSource1>
    <&LayoutTemplate模板GT;
      <表>
        &所述; TR>
            <第i个客户和LT; /第i
            <第i个货号和LT; /第i
        < / TR>
         < ASP:占位符=服务器ID =itemPlaceholder>< / ASP:占位符>
      < /表>
    < / LayoutTemplate模板>
    <&ItemTemplate中GT;
            &所述; TR>
                &所述; TD>
                    <%#的eval(客户名称)%>
                < / TD>
                &所述; TD>
                    <%#的eval(项目)%>
                < / TD>
            < / TR>
    < / ItemTemplate中>
    < / ASP:的ListView>


解决方案

你必须改变你的aspx页面,如下

 < ASP:ListView控件ID =ListView1的=服务器的DataSourceID =ObjectDataSource1>
<&LayoutTemplate模板GT;
  <表>
    &所述; TR>
        <第i个客户和LT; /第i
        <第i个货号和LT; /第i
    < / TR>
     < ASP:占位符=服务器ID =itemPlaceholder>< / ASP:占位符>
  < /表>
< / LayoutTemplate模板>
<&ItemTemplate中GT;
        &所述; TR>
            &所述; TD>
             < ASP:标签ID =lblCustomerName文本='<%#的eval(客户名称)%>' =服务器>< / ASP:标签>
            < / TD>
            &所述; TD>
             < ASP:标签ID =lblItem文本='<%#的eval(项目)%>' =服务器>< / ASP:标签>
            < / TD>
        < / TR>
< / ItemTemplate中>
< / ASP:的ListView>

现在你必须为每个环路低于code使用隐藏文件

 字符串strProductNames =的String.Empty;
的foreach(在ListView1.Items ListViewItem的项目)
    {
        标签lblCustomerName =(标签)item.FindControl(lblCustomerName);       // strProductNames = strProductNames + lblCustomerName.Text +< BR />中;
       //你可以在lblCustomerName.Text得到的值。使用这个值按您的要求
    }

希望这将有助于you..happy编码

I am trying to loop over a ListView with a foreach statement, but I can't seem to get the Subitems of item. No success with a For statement either. IntelliSense doesn't propose it on both ways.

Code Behind:

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>

解决方案

you have to change your aspx page as below

<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>

Now you have to use for each loop as below in code behind file

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
    }

Hope this will helps you..happy coding

这篇关于无法访问子项中的ListView而循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆