Solved

FindControl() With MasterPages

Posted on 2008-10-30
7
661 Views
Last Modified: 2013-11-08
Hello, Im tring to find a control inside a master page contentplaceholder with the following:

                Dim TargetImage As HyperLink
                TargetImage = FindControl("Content2").FindControl("Image" & i)

I get object reference not set to an instance of an object.  I also tried:
                TargetImage = Me.Master.FindControl("Content2").FindControl("Image" & i)

The controls are not in the master page but in a page that uses the master page.  It did work before adding utalizing master pages also.  How can i fix this?

Thanks,

Greg
0
Comment
Question by:grogo21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:dampsey
ID: 22842123
Assuming Content2 is the name of the master page contentplaceholder name,

Can you please try the following:
TargetImage = Me.Master.FindControl("Image" & i)

0
 

Author Comment

by:grogo21
ID: 22842299
That did not work. I have attached the source code for my page.

Thanks!
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <div class="HeaderContentDiv">
            <asp:Label ID="WelcomeLabel" CssClass="WelcomeLabel" runat="server" Text=""></asp:Label><br /></div>
            
        <div class="GalleryContentDiv">
            <table>
                <tr valign="middle">
                    <td><asp:HyperLink ID="Image1" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image2" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image3" runat="server" visible="false" CssClass="ImageTR"/></td>
                </tr>
                <tr class="ImageTR" valign="middle">
                    <td><asp:HyperLink ID="Image4" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image5" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image6" runat="server" visible="false" CssClass="ImageTR"/></td>
                </tr>
                <tr valign="middle" align="center">
                    <td colspan="3" align="center" style="padding:5px; text-align:center;"><asp:HyperLink ID="Banner1" runat="server" visible="false" Target="_blank" CssClass="BannerImg"/></td>
                </tr>
                <tr class="ImageTR" valign="middle">
                    <td><asp:HyperLink ID="Image7" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image8" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image9" runat="server" visible="false" CssClass="ImageTR"/></td>
                </tr>
                <tr class="ImageTR" valign="middle">
                    <td><asp:HyperLink ID="Image10" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image11" runat="server" visible="false" CssClass="ImageTR"/></td>
                    <td><asp:HyperLink ID="Image12" runat="server" visible="false" CssClass="ImageTR"/></td>
                </tr>
....
            </table>
        </div>
</asp:Content>

Open in new window

0
 
LVL 8

Accepted Solution

by:
dampsey earned 500 total points
ID: 22842363
place the below code anywhere in your code.

then use the following

FindControlRecursive(this.Page, "Image" + i)

I know the following is written in c# but you may change it to vb easily
        public static Control FindControlRecursive(Control Root, string Id)
        {
            if (Root.ID == Id)
                return Root;
 
            foreach (Control Ctl in Root.Controls)
            {
                Control FoundCtl = FindControlRecursive(Ctl, Id);
                if (FoundCtl != null)
                    return FoundCtl;
            }
 
            return null;
        }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:grogo21
ID: 22842523
Still get Object reference not set to an instance of an object.

Thanks
0
 
LVL 8

Expert Comment

by:dampsey
ID: 22847962
In which event of the page you are using the FindControl function?
0
 

Author Comment

by:grogo21
ID: 22860202
PageLoad event
0
 

Author Comment

by:grogo21
ID: 22860332
This appears to work:
                TargetImage = DirectCast(DirectCast(Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder).FindControl("Image" & i.ToString), HyperLink)


Thanks!
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL and CLR parameter strings 9 63
Send SMS from vb.net desktop app 30 51
Background worker passing messagebox yes no 13 25
Intermittent OleDbConnection Error 20 52
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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