findcontrol in listview

Posted on 2011-10-02
Last Modified: 2013-12-16

I use a masterpage.

In my content (default.aspx), I want to find the hyperlink control for a certain page - etc. frontpage.
Frontpage is in this listview:
 <asp:ListView ID="lvMenu" runat="server" DataSourceID="dsKategorier">

This dosen´t work, I dont understand:
 HyperLink textBox = (HyperLink)Master.lvMenu.FindControl("TaxFileIDTextBox");
Question by:bongii
    LVL 18

    Accepted Solution

    You will have to FindControl on a ListViewDataItem instead of FindControl on the entire ListView. See the code section below:
    Loop through all rows:
    foreach (ListViewDataItem currentItem in lvMenu)
      HyperLink hp = (HyperLink)currentItem.FindControl("TaxFileIDTextBox");
    Or for a single row (where the ID is known):
    HyperLink hp = (HyperLink)lvMenu.Items[2].FindControl("TaxFileIDTextBox");

    Open in new window

    LVL 35

    Expert Comment

    by:Miguel Oz
    If the list view is in ContentPlaceHolder, you should try:
        ContentPlaceHolder mpContentPlaceHolder;
        TextBox mpTextBox;
        mpContentPlaceHolder =
        if(mpContentPlaceHolder != null)
            mpTextBox =
                (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
            if(mpTextBox != null)
                mpTextBox.Text = "TextBox found!";


    Notice that this only works for static elements that are defined on your page. If you are filling up the list view with dynamic content, it depends where the controls are atteched and how page state is managed


    Author Comment


    HyperLink hp = (HyperLink)lvMenu.Items[2].FindControl("TaxFileIDTextBox");

    This dosen´t work. LvMenu can´t be found. It on the master page?
    LVL 8

    Expert Comment

    FindControl only searches amongst the controls that are immediate children of the container.
    If we write lvMenu.FindControl("TaxFileIDTextBox") then it will check whether any of the immediate child controls of lvMenu has ID = TaxFileIDTextBox or not. If one is found then that control's reference will be returned otherwise it will return null. Please remember FindControl does not search throughout a hierarchy of controls within controls. See more.

    So in your code see which control is the immediate parent of "TaxFileIDTextBox". Is it lvMenu? It won't be.
    So you will have to first do FindControl to reach that parent.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now