Dynamically Created User Controls not Initialising

Discussion in 'ASP .Net Web Controls' started by John Sewell, Sep 3, 2004.

  1. John Sewell

    John Sewell Guest

    Wonder if anyone can point me in the right direction!!

    Basically I have built an ASP.net (VB.net) user control to display
    data and this is comprised of a set of user controls that display
    parts of the page (header, body etc.)

    The problem I have is that this works fine when I just have the
    control on a normal page, but does not work when I try to dynamically
    add them to a page so that I can show a set of document on the same
    page.

    The page control (call it ctlPage) has 2 controls on it (ctlHeader,
    ctlBody).
    I did have to add each control to the area

    '#Region " Web Form Designer Generated Code "'

    in order to reference them in code (so I can call the init method with
    the ID of them to show)

    So ctlPage has a public method "ShowData" which simply calls the
    methods on the controls on the page with the ID passed in.

    The problem is that when control is created dynamically I get an error
    that the controls on ctlPage are not set when I try and call ShowData.
    I get the error:
    "Object reference not set to an instance of an object", the objects
    dont seem to be created.

    The call to create ctlPage dynamically is:
    theNewItem = New ctlPage
    newCell.Controls.Add(theNewItem)
    theNewItem.ShowData(dsData.Tables(0).Rows(lngLoop)("ItemID"))


    Does anyone have an idea what I have done wrong or I am not
    initialising the control(s) correctly?

    Many thanks,

    John
     
    John Sewell, Sep 3, 2004
    #1
    1. Advertising

  2. John,
    When you dynamically load a user control, you must reference the .ascx
    file that defines the user control. Also, you can put your code in the
    Page_Load event handler.
    theNewItem = Page.LoadControl("~/myvirtualpath/mycontrol.ascx") As
    myControlType
    > newCell.Controls.Add(theNewItem)
    > theNewItem.ShowData(dsData.Tables(0).Rows(lngLoop)("ItemID"))


    Do some reading on user controls and object-oriented development in general
    because what you are doing is taking advantage of the object model of your
    custom user control.

    Best regards,
    Jeffrey Palermo

    "John Sewell" <> wrote in message
    news:...
    > Wonder if anyone can point me in the right direction!!
    >
    > Basically I have built an ASP.net (VB.net) user control to display
    > data and this is comprised of a set of user controls that display
    > parts of the page (header, body etc.)
    >
    > The problem I have is that this works fine when I just have the
    > control on a normal page, but does not work when I try to dynamically
    > add them to a page so that I can show a set of document on the same
    > page.
    >
    > The page control (call it ctlPage) has 2 controls on it (ctlHeader,
    > ctlBody).
    > I did have to add each control to the area
    >
    > '#Region " Web Form Designer Generated Code "'
    >
    > in order to reference them in code (so I can call the init method with
    > the ID of them to show)
    >
    > So ctlPage has a public method "ShowData" which simply calls the
    > methods on the controls on the page with the ID passed in.
    >
    > The problem is that when control is created dynamically I get an error
    > that the controls on ctlPage are not set when I try and call ShowData.
    > I get the error:
    > "Object reference not set to an instance of an object", the objects
    > dont seem to be created.
    >
    > The call to create ctlPage dynamically is:
    > theNewItem = New ctlPage
    > newCell.Controls.Add(theNewItem)
    >

    theNewItem.ShowData(dsData.Tables(0).Rows(lngLoop)("ItemID"))
    >
    >
    > Does anyone have an idea what I have done wrong or I am not
    > initialising the control(s) correctly?
    >
    > Many thanks,
    >
    > John
     
    Jeffrey Palermo [MCP], Sep 5, 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. Tim Greenwood
    Replies:
    1
    Views:
    872
    Teemu Keiski
    May 13, 2005
  2. Kees de Winter
    Replies:
    2
    Views:
    513
    Kees de Winter
    Jan 29, 2007
  3. msimmons
    Replies:
    0
    Views:
    514
    msimmons
    Jul 16, 2009
  4. Denise

    Dynamically created user controls

    Denise, Mar 16, 2005, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    127
    Brock Allen
    Mar 16, 2005
  5. Andrew

    Dynamically Created User Controls

    Andrew, Sep 4, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    200
    Ezra Epstein
    Sep 13, 2003
Loading...

Share This Page