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

Discussion in 'ASP .Net Building Controls' started by SteveS, Aug 11, 2004.

  1. SteveS

    SteveS Guest

    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
     
    SteveS, Aug 11, 2004
    #1
    1. Advertising

  2. SteveS

    SteveS Guest

    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


    "SteveS" <> wrote in message
    news:%...
    > 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
    >
    >
     
    SteveS, Aug 11, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    1
    Views:
    5,455
    Christophe Gijbels
    Jan 4, 2004
  2. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    585
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  3. Replies:
    1
    Views:
    1,193
    Nathan Sokalski
    Sep 2, 2007
  4. Dan
    Replies:
    5
    Views:
    375
  5. Dan
    Replies:
    3
    Views:
    847
    Alessandro Zifiglio
    Jan 21, 2004
Loading...

Share This Page