C# 在 ListView 控件中查找控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14903094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:24:35  来源:igfitidea点击:

Find Control Inside ListView Control

c#asp.netexceptionwebforms

提问by TheChampp

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

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

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

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

但我收到此异常:Object reference not set to an instance of an object

What is wrong here ?

这里有什么问题?

This is aspx code:

这是aspx代码:

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

回答by Igoy

It should be Label1 in the arguement:

在争论中应该是 Label1:

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

This should be in a databound event.

这应该在数据绑定事件中。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx

回答by mshsayem

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:

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

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 rowand retrieve the control as:

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

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

回答by Ankit Singh

This function will get Author Name from a database, you just need to call your method to get Author Name and then return it

该函数将从数据库中获取作者姓名,您只需要调用您的方法来获取作者姓名然后返回它

protected string GetUserFromPost(Guid? x)
{
    // call your function to get Author Name
    return "User Name";
}

And to bind label in the list view you have to do it in list view ItemDataBoundEvent

并且要在列表视图中绑定标签,您必须在列表视图ItemDataBound事件中进行

protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label lbl = e.Item.FindControl("Label") as Label;
        lbl.Text = "Active";
    }
}

Here are the list view aspx code changes (just add onitemdatabound="ChatListView_ItemDataBound"):

以下是列表视图 aspx 代码更改(只需添加onitemdatabound="ChatListView_ItemDataBound"):

asp:ListView 
ID="ChatListView" 
runat="server" 
DataSourceID="EntityDataSourceUserPosts" 
onitemdatabound="ChatListView_ItemDataBound" 

回答by Chris Nguyen

Try it:

尝试一下:

protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item is ListViewDataItem)
    {
         var yourLabel = e.Item.FindControl("Label1") as Label;
         // ...
    }
}

回答by Allen

One simple solution to this problem, which avoids the FindControlcode is to place OnIniton your label.

此问题的一个简单解决方案是将FindControl代码放置OnInit在您的标签上。

This would change your page code to this: <asp:Label ID="Label" runat="server" Text="" Visible="True" OnInit="Label_Init"></asp:Label>

这会将您的页面代码更改为: <asp:Label ID="Label" runat="server" Text="" Visible="True" OnInit="Label_Init"></asp:Label>

And in your code behind you will now have a function like this:

在你后面的代码中,你现在将有一个这样的功能:

protected void Label_Init(object sender, EventArgs e)
{
     Label lblMyLabel = (Label)sender;
     lblMyLabel.Text = "My Text";
}