Using FindControl for a control in the ItemTemplate of a ListView

Update: I’m not sure what was going on in my head the day I wrote this article, but as Lukas points out in a comment below you can simply use e.Item.FindControl(“cblRoles”) as apposed to the convoluted way I’m doing it :-)

I’ve been playing around with the ListView control recently and am quite impressed with it.  I like how it gives full control over the markup used as apposed to the other data driven controls.

I came across an issue where in my ItemTemplate I have a CheckBoxList which I needed to reference when I hit a button also in the ItemTemplate.

As the ItemTemplate repeats there will be many CheckBoxLists so how do I find the correct one?

The answer I found was in the ListView’s Items collection. On my button I set the CommandArgument to the DataItemIndex, then in my handler for the ListView’s ItemCommand event I used FindControl on the item matching the index.

Here is my item template:

<ItemTemplate>
<div>
<asp:Literal ID="lblForename" Text='<%# Eval("Forename") %>' runat="server" />
<asp:Literal ID="lblSurname" Text='<%# Eval("Surname") %>' runat="server" />
�
<div>
<asp:CheckBoxList   ID="cblRoles"
DataSourceID="odsRole"
DataValueField="ID"
DataTextField="Name"
OnDataBound="cblRoles_OnDataBound"
runat="server">
</asp:CheckBoxList>
</div>
�
<div>
<asp:Button ID="btnSaveRoles"
Text="Save Roles"
CommandName="SaveRoles"
CommandArgument='<%# Container.DataItemIndex %>'
runat="server" />
</div>
</div>
</ItemTemplate>

And here is my event handler:


protected void lvUser_ItemCommand(object sender, ListViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("SaveRoles"))
{
CheckBoxList cblRoles = lvUser.Items[Convert.ToInt32(e.CommandArgument)].FindControl("cblRoles") as CheckBoxList;

//Other code
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}

So I’m using the FindControl method on the ListViewDataItem in the position passed via the CommandArgument which gives me the correct control.

Posted on by Joe in ASP.NET, C#

14 Responses to Using FindControl for a control in the ItemTemplate of a ListView

  1. Pingback: Access a control by ID From Within a Databound Control

  2. David

    I just came across this page today, and it was a life-saver! I was having issues finding controls in a ListView ItemTemplate, and this solved it!

  3. Joe

    Glad it helped mate

    Joe

  4. Ngala Talla

    Thanks Joe . This piece was Just what I was looking for. I will really want to know why it works . Continue with the good work
    My regards

  5. Lukas

    You could also use e.Item.FindControl(“cblRoles”).

    This will find the control in the item that caused the ItemCommand event.

  6. Ryan

    I tried it and it worked great, however when I added a DataPager to the ListView, upon clicking an item on page 2 it threw an error. Any suggestions?

  7. Joe

    Lukas: Thanks for that, not sure how I missed that property

    Ryan: Just use e.Item.FindControl(“cblRoles”) as Lukas suggested and you won’t get the issue with the DataPager

    Cheers
    Joe

  8. alchemystic

    Thank you, you save me a lot of time with your post.

  9. peyman

    Thanks a lot. This code saved my life::)))

  10. priya

    Thanks a lot…this code is very usefull for me.

  11. Alejandro

    Excelente aporte, muchas gracias!!

  12. Phil

    Great post searched for ages using recursive etc which didn’t do what I wanted.
    Whilst your initial solution didn’t do what I wanted your updated comment using other version was on the button. Many thanks

  13. web designing in Agra

    Thanks a lot, this post is very helpful for me to learn how to find a control from listview in ASP .Net
    Thanks Again……..

  14. Med

    Thks a lot
    Verry usefull script !

Add a Comment