Solved

Having trouble with FindControl

Posted on 2010-11-16
4
414 Views
Last Modified: 2012-05-10
I have an application where I need to perform operations on various controls throughout the page based on my repeater data.  I need to populate Label Text, set Visible to true/false, etc.

The basic structure (framework) of my page is as such:

Repeater 1
>> Label 1
>> Literal 1

>> Nested Repeater
>> >> Label 2
>> >> Hyperlink 2

>> >> >> Nested Nested Repeater
>> >> >> Hyperlink 3
>> >> >>  END nested Repeater

>> END nested Repeater

END Repeater1

I am having an impossible time knowing
(1) When to check for the control (i.e. what can I do during Page_Load, or DataBind, etc), and
(2) the syntax of the control  (i.e.  (Label)Page.FindControl("controName");   OR  (Label)This.FindControl("controlName")  OR (Label)e.Item.FindControl("controlName")

The error I keep getting is "Object reference not set to an instance of an object"

Can anyone clear this up for me?
0
Comment
Question by:vcbertini
[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 Comments
 
LVL 21

Accepted Solution

by:
Alfred1 earned 500 total points
ID: 34151332
Why don't you try using a FindControlRecursive function to locate your control?

Check my comments on this EE links:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_25738395.html

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_25836779.html
0
 
LVL 6

Expert Comment

by:AlfredRobot
ID: 34151337
If you can provide your aspx file and source code, it will be easier for us to help you.
And if you want to find controls inside the repeater, page.findcontrol() will not help. e.item.findcontrol() is a good one, but still, life will be easier if you can provide source code.

Generally, this article is a very good one concerning "FindControl" in asp.net
http://odetocode.com/Articles/116.aspx
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34152144
I always have the same problem. if you don't want to do the recursive thing then start in the Page_Load, and use this to get the top level control.  
 
Control pageControl = ((Control)sender).Parent;

Then using the debugger you can work your way down to the control you are interested in. Once you find it, in the debugger, you will then know how to code your way down to it. In your code, check for null at each step to avoid the "object reference not set" problem.  

0
 

Author Closing Comment

by:vcbertini
ID: 34159078
That seems to work - took an example of the code off of a different post.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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