Object reference not set to an instance of an object and INamingContainer

A list of standard ASP.NET web controls that implement INamingContainer

Any control that implements the INamingContainer interface creates a new control namespace which ensures that child controls have unique names on the page. If you try to directly access, for example, a TextBox within a FormView without using the FormView's FindControl() method, you will receive an "Object reference not set to an instance of an object" error. An excellent explanation of this behaviour and the FindControl() method is provided by K Scott Allen, and I won't attempt to replicate the basis of his article here. Instead, here is a list of all the standard ASP.NET web controls (System.Web.UI.Controls) that implement INamingContainer with which the FindControl method needs to be used:

  • ChangePassword
  • CheckBoxList
  • CreateUserWizard
  • DataGrid
  • DataList
  • DetailsView
  • FormView
  • GridView
  • ListView
  • Login
  • LoginView
  • LoginStatus
  • Menu
  • PasswordRecovery
  • RadioButtonList
  • SiteMapPath

There is one addition to the above list - ContentPlaceHolder - when working with MasterPages. For example, to reference a label in a FormView in a ContentPlaceHolder, you need to do something like the following:

Label lbl = this.Content1.FindControl(""FormView1").FindControl("Label1") as Label;

Date Posted:
Last Updated:
Posted by:
Total Views to date: 26963

4 Comments

- jkh

Error 8 'Controls' is not a member of 'DataGridTest.iX.Controls.DatePicker'. c:\inetpub\wwwroot\DataGridTest\App_Code_Old\datepicker.vb 21 32

- Mike

@jkh

As I mentioned next to the comment form you filled in, please make sure that your comments are relevant to the topic being discussed.

I would have emailed you privately with more advice, but your obviously made-up random email address made that impossible. Bit of a waste of your time, really.

- yogesh

thx so much

- ramz

i cannot find the control rangevalidator in formview asp.net using findcontrol.
Add your comment

If you have any comments to make about this article, please use this form to do so. Make sure that your comment relates specifically to the article above. More general comments can be posted through the form on the Contact page.

Please note, all comments are moderated, and I end up deleting quite a lot. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Requests to fix your code (post a question to forums.asp.net instead, please)
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam

I do not pass email addresses on to spammers, so a valid one will assist me in responding to you personally if required.

Recent Comments

hosein ey 2/25/2015 1:56 PM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
tnx for this article do you think next version of entityframework support's the sql server and ?...

Saywer Ford 2/25/2015 5:15 AM
In response to Optimising ASP.NET Web Pages Sites - Bundling And Minification
Great Article. I did everything right and working fine. How about page specific js files. Lets I...

Saravanan 2/24/2015 10:54 AM
In response to Optimising ASP.NET Web Pages Sites - Bundling And Minification
Hi, Great article about the Bundling and minification. Regards the caching as you mentioned the...

Justin 2/24/2015 10:43 AM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
I'm having the same problem as Ingmar. Everything is working fine locally in Visual Studio, but I it...

Logan Mudlo 2/23/2015 4:59 PM
In response to WebMatrix - A First Application
Is there a way to prevent the automatic close on a Database.Open() call?...

Mog0 2/23/2015 11:16 AM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
Probably a silly question but why did you disable cascade delete and implement it yourself?...

Jose-Maria 2/19/2015 3:29 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Views and Controllers
Just great ! Many thanks Mike....

Satyabrata Mohapatra 2/19/2015 12:02 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Views and Controllers
This is great!!...

Ryan Helmoski 2/19/2015 10:24 AM
In response to Conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value
Thank you!...

Harsha 2/19/2015 8:27 AM
In response to iTextSharp - Adding Text with Chunks, Phrases and Paragraphs
Hi, Can text area be created so that I can type text in the area in pdf document? We have Add text...