查找内部控制ListView控件 [英] Find Control Inside ListView Control

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

问题描述

我要找到ListView控件内部控制与ID =标签标签的控制。我试着用下面的code要做到这一点:

 ((标签)this.ChatListView.FindControl(标签))文本=活动。

但我得到这个异​​常:对象引用未设置为一个对象的实例。

什么是错在这里?

这是ASPX code:

 < ASP:ListView控件ID =ChatListView=服务器的DataSourceID =EntityDataSourceUserPosts>
    <&ItemTemplate中GT;
        < D​​IV CLASS =后>
            < D​​IV CLASS =postHeader>
                < H2>< ASP:标签ID =Label1的=服务器
                    文本='&下;%#评估和演示(名称)+由+ this.GetUserFromPost((GUID)的eval(AuthorUserID)?)%GT;' >< / ASP:标签>< / H>
                < ASP:标签ID =标签=服务器文本=可见=真>< / ASP:标签>
                < D​​IV CLASS =dateTimePost>
                   <%#的eval(踵)%>
                < / DIV>
            < / DIV>
            < D​​IV CLASS =postContent>
                <%#的eval(PostComment)%>
            < / DIV>
        < / DIV>
    < / ItemTemplate中>< / ASP:的ListView>


解决方案

列表视图是数据绑定控件;所以它里面的控件将有不同的行不同的ID。你必须首先检测的行,然后抢了控制。最佳抢这种控制是像 OnItemDataBound 事件中。在那里,你可以做到这一点抓住你的控件:

 保护无效myListView_ItemDataBound(对象发件人,ListViewItemEventArgs E)
{
    如果(e.Item.ItemType == ListViewItemType.DataItem)
    {
        VAR yourLabel = e.Item.FindControl(标签1)作为标签;        // ...
    }
}

如果你想抓住它的Page_Load ,你必须知道的特定行的和检索控制为:

  VAR theLabel = this.ChatListView.Items [<&ROW_INDEX GT]。的FindControl(标签1)作为标签;

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

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