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

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

Mike 6/29/2015 2:22 AM
In response to MVC 5 with EF 6 in Visual Basic - Sorting, Filtering and Paging
This is the first example that I have found that works....

Marty 6/28/2015 4:57 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Mike, what if I don't want to render back the text to the browser, but I want to send it some other...

Mike 6/27/2015 4:00 PM
In response to Migrating Classic ASP To ASP.NET Razor Web Pages Part One- Razor Syntax And Visual Basic
have you used any of the code converters to convert classic asp to c#? If so, which one do you have...

deepan 6/27/2015 12:49 PM
In response to How To Send Email In ASP.NET MVC
Your article is very very useful for me...thanks for giving this article ...this is very simple to u...

Howie 6/27/2015 6:20 AM
In response to Scheduled Tasks In ASP.NET With Quartz.Net
Mike - Great article. I'm curious about the lifetime of the scheduler - I see your is static and you...

Chris 6/26/2015 7:42 PM
In response to Ajax with Classic ASP using jQuery
I have searched the web high and low for a clean simple solution using JQuery, JSON, and Classic ASP...

aaron 6/26/2015 12:01 PM
In response to 7 C# 6.0 Features That Every ASP.NET Developer Should Know About
#6 enables a less-verbose functional programming style...

Marty 6/26/2015 5:26 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
Great article. You don't show the code in Receiver.cshtml to send data back. How would you send text...

Vimal 6/25/2015 11:09 AM
In response to iTextSharp - Working with Fonts
I want to edit my existing PDF. I want to change font color. I want to change is gray or some I to...

yoyo 6/24/2015 9:00 PM
In response to Dependency Injection and Inversion of Control with ASP.NET MVC
Hi, thanks for your explainations. Lionel...