Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

findcontrol in listview

Posted on 2011-10-02
4
Medium Priority
?
874 Views
Last Modified: 2013-12-16
Hi.

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");
0
Comment
Question by:bongii
4 Comments
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 2000 total points
ID: 36900708
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

0
 
LVL 36

Expert Comment

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

Check:
http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx

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

0
 

Author Comment

by:bongii
ID: 36902528
matthewstevenkelly:

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

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

Expert Comment

by:jagrut_patel
ID: 36908223
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.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 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