Can't find control

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I added a content place holder (ID="cphContent") to a master page.

Now I need to add controls to that content place holder in the pages
which use that master page.

I am doing it at runtime as follows

Dim cphContent As ContentPlaceHolder =
CType(Page.FindControl("cphContent"), ContentPlaceHolder)
Dim MyLabel As New Label
AddHandler MyLabel.Init, AddressOf MyLabel_Init
cphContent.Controls.Add(MyLabel)

I am getting an error:

"System.NullReferenceException: Object reference not set to an
instance of an object."

I think I am getting the error because my the cphContent content place
holder is not found.

Could you tell me how to solve this?

Thanks,

Miguel
 
Hi,
I added a content place holder (ID="cphContent") to a master page.

Now I need to add controls to that content place holder in the pages
which use that master page.

I am doing it at runtime as follows

Dim cphContent As ContentPlaceHolder =
CType(Page.FindControl("cphContent"), ContentPlaceHolder)
Dim MyLabel As New Label
AddHandler MyLabel.Init, AddressOf MyLabel_Init
cphContent.Controls.Add(MyLabel)

I am getting an error:

"System.NullReferenceException: Object reference not set to an
instance of an object."

I think I am getting the error because my the cphContent content place
holder is not found.

is your ContentPlaceHolder itself contained within another control? You'll
need to specify the complete "path" to your CPH. In other words, if you use
FindControl with nested controls, you'll have to do a recursive search.

Cheers,
Olaf
 
This probably won't solve your problem, but it's worth a shot...

Change that line to: CType(FindControl("cphContent"),
ContentPlaceHolder)
 
Back
Top