开发者

How to pull a type of item from a list view

More specificly I want to be able to gather the Labels I have in my ListView's Item template into a list.

This was my latest attempt at it and sadly I am still falling short.

foreach (Label lbl in ListView.Items.Where(r => r.ItemType == Label))

This is my ASP Control:

<asp:ListView ID="ListView1" runat="server" Visible="false" ItemPlaceholderID="phItem"
    OnItemDataBound="ListView1_ItemDataBound">
    <LayoutTemplate>
        <html>
        <head>
        </head>
        </head>
        <body>
            <table>
                <asp:PlaceHolder ID="phItem" runat="server" />
            </table>
        </body>
        </html>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="lblChecklist" runat="server" />
            </td>
            <td>
                <asp:Label ID="lblCompliant" runat="开发者_运维问答server" />
            </td>
            <td>
                <asp:Label ID="lblNonCompliant" runat="server" />
            </td>
            <td>
                <asp:Label ID="lblNotApplicable" runat="server" />
            </td>
            <td>
                <asp:Label ID="lblNotComplete" runat="server" />
            </td>
            <td>
                <asp:Label ID="lblTotal" runat="server" />
            </td>
        </tr>
    </ItemTemplate>
    <EmptyDataTemplate>
        There are not results that match your input paramaters.
    </EmptyDataTemplate>
</asp:ListView>

I've been looking to pull the Labels and now when I look at ListView1.Items during runtime I find the ListView1.Items.Count = 0.

I can pull the Labels individually by their name but I want to pull them so I can get out of hardcoding it.


I think the LINQ statement you want is something like:

var myLabels = listView1.Items
    .Where(i => i.ItemType == ListViewItemType.DataItem)
    .Select(a => a.FindControl("myLabel"));

You want to use FindControl on each item to get to the Label control you added in the ItemTemplate. Remember that Items is just a list of ListViewDataItems, so you won't be able to select the labels right out of it, even if they are the only thing in the template.

Here's a full working example I threw together.

<%@ Page Language="C#" AutoEventWireup="true" %>
<html>
<body>
    <form id="form1" runat="server">
        <asp:ListView ID="listView1" runat="server">
            <ItemTemplate>
                <asp:Label ID="myLabel" Text="<%# Container.DataItem %>" runat="server" /><br />
            </ItemTemplate>
        </asp:ListView>
        <asp:Button ID="btnGatherLabels" Text="Gather Labels" OnClick="btnGatherLabels_Click" runat="server" />
    </form>
</body>
</html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            var myItems = new[] { "item1", "item2", "item3" };
            listView1.DataSource = myItems;
            listView1.DataBind();
        }
    }

    public void btnGatherLabels_Click(object sender, EventArgs e)
    {
        var myLabels = listView1.Items
                        .Where(i => i.ItemType == ListViewItemType.DataItem)
                        .Select(a => a.FindControl("myLabel"));
        foreach(Label myLabel in myLabels)
            Response.Write(string.Concat(myLabel.Text, "<br />"));
    }
</script>

EDIT
Building off my earlier example, let's say the ItemTemplate has multiple labels:

<ItemTemplate>
    <asp:Label ID="myLabel1" Text="Label Here:" runat="server" /> 
    <asp:Label ID="myLabel2" Text="<%# Container.DataItem %>" runat="server" /> 
    <asp:TextBox ID="filler" runat="server" /><br />
</ItemTemplate>

The following code will bring back and display the text for each label in each template:

public void btnGatherLabels_Click(object sender, EventArgs e)
{
    var myLabelCols = listView1.Items
        .Where(i => i.ItemType == ListViewItemType.DataItem)
        .Select(a => 
            a.Controls.Cast<Control>()
                .Where(b => b.GetType().Equals(typeof(Label)))
        );
    foreach (var myLabels in myLabelCols)
        foreach (Label myLabel in myLabels)
            Response.Write(string.Concat(myLabel.Text, "<br />"));
}

I'm not sure how you need to use this data, so I did not join them together in one list. Each element in the myLabelCols contains a list of all of the labels in that template.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜