User Control. How is this done?

S

shapper

Hello,

I have been trying to add a user control at runtime to a page:

Dim myUserControl As Control = LoadControl("MyUserControl.ascx")

The problem is this does not give me access to the user control
properties. So I tried the following:

Dim myUserControl As MyUserControl =
CType(LoadControl("MyUserControl.ascx"), MyUserControl)

However MyUserControl is not recognized.

How can solve this?

I looked everywhere and I can't find the solution.

Thanks,
Miguel
 
K

Karl Seguin

In 2.0, you need to add a @Reference directive atop your page...somethng
like:

<%@Reference Control="~/controls/user.ascx" %>

which'll then make the type available in codebehind.

Karl
 
S

shapper

Hello,

But why do I need a reference in my master page if I am doing
everything in my master page code behind code?

Thanks,
Miguel
 
K

Karl Seguin

The new compilation model in 2.0 makes every page+codebehind get compiled
into it's own assembly.

Just like any other assemblies, you can't use them unless they are
referenced in your project.

Since the assembly name is dynamic, you use the @reference attribute to hook
it up for it.

I don't like it, but it's how it works..

Karl
 
S

shapper

Karl,

I did add the reference and I still have the problem.
I can't access my user control properties in the code behind code of
where I add it.

Any idea?

Thanks,
Miguel
 
S

shapper

Hi Karl,

I solved it using another way which I prefer.
I place all user controls inside the namespace MyWebSite.
Then I add then namespace in Web.Config.

I prefer this way. It is working ... for now :)

Thanks,
Miguel
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top