Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Control Inside ListView Control

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>
like image 552
TheChampp Avatar asked Feb 15 '13 20:02

TheChampp


5 Answers

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;
like image 66
mshsayem Avatar answered Oct 12 '22 09:10

mshsayem


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" 
like image 25
Ankit Singh Avatar answered Oct 12 '22 08:10

Ankit Singh


It should be Label1 in the arguement:

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

like image 38
Igoy Avatar answered Oct 12 '22 09:10

Igoy


Try it:

protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item is ListViewDataItem)
    {
         var yourLabel = e.Item.FindControl("Label1") as Label;
         // ...
    }
}
like image 32
Chris Nguyen Avatar answered Oct 12 '22 09:10

Chris Nguyen


One simple solution to this problem, which avoids the FindControl code is to place OnInit on your label.

This would change your page code to this: <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";
}
like image 23
Allen Avatar answered Oct 12 '22 07:10

Allen