Find Control Inside ListView Control

前端 未结 5 1167
有刺的猬
有刺的猬 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:29

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

    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"):

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

提交回复
热议问题