Dynamically added user control throws object not set

  • Thread starter Thread starter andyrich_1
  • Start date Start date
A

andyrich_1

HI,

I'm adding a usercontrol to a webform in the following way


Dim ctlRate As New ctlRatesTable
ctlRate.SomeMethod()
Me.Page.Controls.Add(ctlRate)

Where sub routine "SomeMethod" attempts to set the text property of a
lable webcontrol. The problem is that the lable hasn't been created.
I've tried calling EnsureChildControls() before using the lable control
but this hasn't helped.

Any ideas what I'm doing wrong?

Thanks,
Andy
 
Try putting it also like this:

***
Dim ctlRate As New ctlRatesTable

' FOLLOWING TWO LINES IN DIFFERENT ORDER THAN ORIGINALLY
Me.Page.Controls.Add(ctlRate)
ctlRate.SomeMethod()

And yes, inside ctlRatesTable, before accessing child controls make sure
EnsureChildControls is called (and child controls instantiated in
CreateChildControls method)
 
Back
Top