Object reference not set to an instance of an object and 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:
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;
Currently rated 3.67 by 12 people
21 August 2007 11:36
Last Updated: 22 May 2010 19:51
Posted by: Mikesdotnetting
Total Views to date: 17539
Unfortunately, something went wrong and your message or comments have not been submitted successfully. I'll try to fix whatever the problem is as soon as I can.
Thanks for your comments. They have been successfully sent to me. It may take a while before I review them for publishing, so please check back again.