Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dynamically Created User Controls not Initialising

Reply
Thread Tools

Dynamically Created User Controls not Initialising

 
 
John Sewell
Guest
Posts: n/a
 
      09-03-2004
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
 
Reply With Quote
 
 
 
 
Jeffrey Palermo [MCP]
Guest
Posts: n/a
 
      09-05-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Cannot access dynamically created user controls Kees de Winter ASP .Net 2 01-29-2007 03:20 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Dynamically created user controls Denise ASP .Net Building Controls 4 03-16-2005 06:55 PM
Dynamically Created User Controls Andrew ASP .Net Web Controls 1 09-13-2003 06:00 PM



Advertisments