Can't find control

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
 
O

Olaf Rabbachin

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
 
P

Phil Wilks

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top