Problem setting a property of a user control

K

Karl Lang

Hi, I'm trying to set a property of a user control that is part of another
user control. So I have a control A which contains control B, and I'm trying
to set a property in B from within the codebehind file for A.

In control codebehind for A in page_load :
Protected objButtons As ProductButtons

objButtons = DirectCast(FindControl("ProductButtons1"), ProductButtons)
objButtons.ProductID = 8

In the ascx file for Control A
<PRODUCTBUTTONSCTRL:pRODUCTBUTTONS id="ProductButtons1" runat="server"
instance="1"/>

Control B has a property called ProductID

When I run the page I get "Object reference not set to an instance of an
object" on the line objButtons.ProductID = 8

Any ideas on how to get around this? can I reference the sub control B from
within the aspx page that holds control A?

klang
 
J

John Saunders

Karl Lang said:
Hi, I'm trying to set a property of a user control that is part of another
user control. So I have a control A which contains control B, and I'm trying
to set a property in B from within the codebehind file for A.

In control codebehind for A in page_load :
Protected objButtons As ProductButtons

objButtons = DirectCast(FindControl("ProductButtons1"), ProductButtons)
objButtons.ProductID = 8

In the ascx file for Control A
<PRODUCTBUTTONSCTRL:pRODUCTBUTTONS id="ProductButtons1" runat="server"
instance="1"/>

Control B has a property called ProductID

When I run the page I get "Object reference not set to an instance of an
object" on the line objButtons.ProductID = 8

Any ideas on how to get around this? can I reference the sub control B from
within the aspx page that holds control A?

In general, you should have control A expose a property which simply sets or
gets the corresponding property of control B. That is, the property in A
should delegate to B.
 
K

Karl Lang

John Saunders said:
In general, you should have control A expose a property which simply sets or
gets the corresponding property of control B. That is, the property in A
should delegate to B.

Thanks for the pointer John.
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top