How do I dynamically add a user control to a web page?

S

SteveS

I have a user control which I created that does not work when I add it
dynamically to a web page. My code looks like this.

For x = 0 to max
'Instantiate a new conrol
Dim ctrlMyNewControl as new MyNewControl
'Assign the application Id
ctrlMyNewControl.ApplicationId = x
'Add the control to the current web page.
plcPlaceHolder.Controls.Add(ctrlMyNewControl)
Next

I get the following error in my control: Object reference not set to an
instance of an object.
On the line: lblApplicationId.Text = _applicationId

For some reason lblApplicationId is set to nothing even though it is defined
on the .ascx page. I have never added a user control which I created
dynamically. I believe I coded my user control correctly, because it works
when I add it directly to the web page.

Am I creating the dynamic control wrong???

Thank you very much for you help!

Steve
 
S

SteveS

Here is the problem.. My code for loading the control should be this:

For x = 0 to max
'Instantiate a new conrol
Dim ctrlMyNewControl as MyNewControl
'LOAD the control
ctrlMyNewControl = CType(LoadControl("\UserControls\MyNewControl.ascx"),
MyNewControl)
'Assign the application Id
ctrlMyNewControl.ApplicationId = x
'Add the control to the current web page.
plcPlaceHolder.Controls.Add(ctrlMyNewControl)
Next
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top