Find Control Inside ListView Control

前端 未结 5 1179
有刺的猬
有刺的猬 2020-12-19 08:06

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


        
5条回答
  •  被撕碎了的回忆
    2020-12-19 08:27

    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[].FindControl("Label1") as Label;
    

提交回复
热议问题