HowTo: Dynamically Add Custom Controls?

D

David Freeman

Hi There!

I'm having trouble dynamically adding custom controls. My custom controls
does not use code-behind but only <script /> block for programming. So as a
result, I don't need to compile my custom controls to dll for them to work.

But I wonder, if I don't compile to dll, can I still dynamically add that
custom control? If so how? Below is how I've tried (and failed).

At top of the page...
<%@ Register TagPrefix="MyControls" Src="DynamicTest.ascx"
TagName="DynamicTest" %>


In the <script /> block...

Sub Page_Load(Sender As Object, e As EventArgs)

Dim objNew as New DynamicTest '<< Raised Error: BC30002: Type
'DynamicTest' is not defined

Controls.Add(objNew)

End Sub

What I'm doing wrong here? I'm really hitting my head against the wall. Plz,
anyone could help, I really appreciate it!

David
 
R

Robert Koritnik

Ok. First of all you are not creating custom control but a user control.
User control is NEVER a separate DLL but a part of the application DLL.

User controls consist of ascx and a codebehind. If you just want to make an
instance of the codebehind the codebehind doesn't know what is its ASCX
file. Dynamic loading of USER controls should be run with LoadControl()
method which you provide an ASCX file, which on the other hand has the
information about the codebehind to correctly load the class from the DLL to
handle it's content.
 
M

Matt Berther

Hello David,

Instead of Dim objNew As New DynamicTest, try this:

Controls.Add(Page.LoadControl("DynamicTest.ascx"))
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top