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: 27586

5 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.

- Afzaal Ahmad Zeeshan

Hello Mike,

Get article to share views over how to ignore null exception in Web Forms.

Anyways, there is a typo mistake in the code section. You've written two double quotes; (""FormView1"). Remove any one in the beginning. :)

Recent Comments

KrASh 5/19/2015 11:10 PM
In response to Adding Search
Hello Mike, I have a question about the second search. In one line it has a where with the title and...

KrASh 5/19/2015 11:04 PM
In response to Accessing Your Model's Data from a Controller
Thanks, I had some silly mistake, can't remember now what it was (had some personal problems last i...

Satyabrata Mohapatra 5/18/2015 4:29 PM
In response to Error Handling in ASP.NET Razor Web Pages
Great article. Learned a lot....

Corstian Boerman 5/13/2015 12:59 PM
In response to ASP.NET 5 Middleware, Or Where Has My HttpModule Gone?
It looks as if this technique doesn't work anymore. (As of the VS 2015 RC release) When adding it...

Alvin 5/12/2015 1:20 PM
In response to Razor Web Pages E-Commerce - Adding A Shopping Cart To The Bakery Template Site
Maybe ability to update quantity of the items once you are in the "Review Cart" step. Also, if on...

Rakesh Panchal 5/12/2015 5:02 AM
In response to ASP.NET MVC 5 with EF 6 - Working With Files
this is v v good web site for help...

Mariusz 5/11/2015 7:19 PM
In response to Introducing TagHelpers in ASP.NET MVC 6
In your comparison of old Html.RouteLink and new AnchorTagHelper where we can specify "title" and in...

Andrea 5/11/2015 3:40 PM
In response to Save And Retrieve Files From a Sql Server CE Database with WebMatrix
Please excuse my noob question. I am using your tips to add file upload to a form I have created. a...

Luis 5/10/2015 5:26 PM
In response to Accessing Your Model's Data from a Controller
Ok. I got it! Return RedirectToAction("Create") on the controller :-) thanks...

Luis 5/10/2015 4:08 PM
In response to Accessing Your Model's Data from a Controller
Hi. Great tutorial. I'm starting to learn MVC. What can I do for when the Create Button is it...