在 ListView 控件中查找控件 [英] Find Control Inside ListView Control

查看:24
本文介绍了在 ListView 控件中查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ListView"控件中找到 ID =Label"的Label"控件.我试图用以下代码来做到这一点:

((Label)this.ChatListView.FindControl("Label")).Text = "active";

但我收到此异常:未将对象引用设置为对象的实例.

这里有什么问题?

这是aspx代码:

<asp:ListView ID="ChatListView" runat="server" DataSourceID="EntityDataSourceUserPosts"><项目模板><div class="post"><div class="postHeader"><h2><asp:Label ID="Label1" runat="server"Text='<%# Eval("Title") + " by " + this.GetUserFromPost((Guid?)Eval("AuthorUserID")) %>'></asp:标签></h2><asp:Label ID="Label" runat="server" Text="" Visible="True"></asp:Label><div class="dateTimePost"><%# Eval("PostDate")%>

<div class="postContent"><%# Eval("PostComment") %>

</ItemTemplate></asp:ListView>

解决方案

Listview 是一个数据绑定控件;所以里面的控件对于不同的行会有不同的id.您必须首先检测该行,然后获取控件.最好在OnItemDataBound 之类的事件中获取此类控件.在那里,您可以这样做以获取控制权:

protected void myListView_ItemDataBound(object sender, ListViewItemEventArgs e){if (e.Item.ItemType == ListViewItemType.DataItem){var yourLabel = e.Item.FindControl("Label1") 作为标签;//...}}

如果你想在 Page_Load 中抓取它,你必须知道特定的行并检索控件为:

var theLabel = this.ChatListView.Items[].FindControl("Label1") as Label;

I want to find "Label" control with ID = "Label" inside the "ListView" control. I was trying to do this with the following code:

((Label)this.ChatListView.FindControl("Label")).Text = "active";

But I am getting this exception: Object reference not set to an instance of an object .

What is wrong here ?

This is aspx code:

<asp:ListView ID="ChatListView" runat="server" DataSourceID="EntityDataSourceUserPosts">
    <ItemTemplate>
        <div class="post">
            <div class="postHeader">
                <h2><asp:Label ID="Label1" runat="server" 
                    Text= '<%# Eval("Title")  + " by " + this.GetUserFromPost((Guid?)Eval("AuthorUserID")) %>' ></asp:Label></h2>
                <asp:Label ID="Label" runat="server" Text="" Visible="True"></asp:Label>
                <div class="dateTimePost">
                   <%# Eval("PostDate")%>
                </div>
            </div>
            <div class="postContent">
                <%# Eval("PostComment") %>
            </div>
        </div>
    </ItemTemplate>

</asp:ListView>

解决方案

Listview is a databound control; so controls inside it will have different ids for different rows. You have to first detect the row, then grab the control. Best to grab such controls is inside an event like OnItemDataBound. There, you can do this to grab your control:

protected void myListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        var yourLabel = e.Item.FindControl("Label1") as Label;

        // ...
    }
}

If you want to grab it in Page_Load, you will have to know specific row and retrieve the control as:

var theLabel = this.ChatListView.Items[<row_index>].FindControl("Label1") as Label;

这篇关于在 ListView 控件中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆