Welcome to MSDN Blogs Sign in | Join | Help

Accessing Controls in ListView Templates

Today when doing some application building I came across a case where I wanted to embed a ASP control inside one of the template of the asp:ListView control. This is legal and simple todo, however it has some interesting effects. Since the control is embedded in a template it is not made available in the code behind file and in my case I wanted to be able to access the control in the code behind file. Here is an example of what my markup looked like:

<asp:ListView ID="ListView1" runat="server">
    <
LayoutTemplate>
        <
table>
            <
tr>
                <
td><asp:PlaceHolder ID="ItemPlaceHolder" runat="server"></asp:PlaceHolder></td>
            </
tr>
            <
tr>
                <
td>
                    <
asp:Button ID="ButtonTest" runat="server" Text="Button" />
                </
td>
            </
tr>
        </
table>
    </
LayoutTemplate>
</
asp:ListView>

I searched the Internet for a solution to this problem and many of the common solutions to the problem I found where to hooked to ItemCreated or DataBound event from the ListView, then using FindControl to find the control and store it in a variable that was part of the page class. While this is entirely doable it leads to really clunky code and there is a much better method for doing this. Controls have the same life cycle methods that the page itself has an by handling the Init event for the button I can get access to the button as soon as it is created and store a copy off that can be used when other parts of the page fire events.

Here is the change to the markup above:

<asp:Button ID="ButtonTest" runat="server" Text="Button" oninit="ButtonTest_Init" />

And here is what the code behind looks like:

public partial class _Default : System.Web.UI.Page {
    Button ButtonTest;

    protected void ButtonTest_Init(object sender, EventArgs e) {
        ButtonTest = (Button)sender;
        ButtonTest.Text = "Foo";
    }

    protected void ListView1_ItemDeleted(object sender, ListViewDeletedEventArgs e) {
        ButtonTest.Text = "Add";
    }

}

The ButtonTest_Init event handler stores the button in ButtonTest and changes it's text. You will also note that because I've done this I can now access the button inside other event handlers the ListView may fire, which was my initial goal. All in a very clean manner not hooking ListView events and not calling FindControl.

Published Wednesday, March 26, 2008 1:09 AM by scothu
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings &raquo; Accessing Controls in ListView Templates

# re: Accessing Controls in ListView Templates

This is the closest I've come to finding a solution to my own problem.  I have two labels in the listview templates.  I've bound data to these labels using the <%#Eval()> thingy.  The question I should be asking is how to get these bound values in the code behind.  

Thursday, August 07, 2008 8:44 PM by Garnett Clarke

# re: Accessing Controls in ListView Templates

Excellent work.  Your approach is infinitely easier (and cleaner) than littering one's code with FindControl calls.

Thursday, December 04, 2008 9:48 AM by Tom Bell

# re: Accessing Controls in ListView Templates

good idea, its better than findcontrol

Tuesday, October 20, 2009 7:23 AM by holgi

# re: Accessing Controls in ListView Templates

Percect, thanks for this solution!

Tuesday, November 03, 2009 11:31 PM by Mosse

# re: Accessing Controls in ListView Templates

Could you supply a VB  version of this code ?

Wednesday, November 04, 2009 2:07 AM by Mark Newton

# re: Accessing Controls in ListView Templates

Excellent, thank you for posting!

Took a lot of searching to get here - I was trying to populate DropDownLists within an InsertItemTemplate on page load and this cleanly solves the problem.

Wednesday, December 09, 2009 9:01 AM by v

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker