views:

127

answers:

3

Is there a way in .NET 2.0 to use a class as a class factory for user controls? In other words, 1 super that sits there and when I want a user control, it creates it and returns it to me. It seems it needs the ASP namespace which I can't seem to get it to reference.

I have a masterpage with a placeholder. Depending upon the user's authentication, I want to dynamically load the user control. For example, it might be a search control if they are authenticated, it might be an ad if they're not logged in. I can dynamically load them. But my plan was to use a single class to control this. Right now I have to load them right on the masterpage code behind. There is no code to show in the class because it won't let me reference the user controls, so I don't get far.

+1  A: 

User controls are meant to be simple chunks of page content. They are not meant to be fancy. You may find more trouble doing this with user controls than is worth doing.

John Saunders
A: 

You can dynamically create controls (I have mainly done it for Sharepoint sites and occasionally by overloading CreateChildControls on a page), but what you gain from this is a pile of page-lifecycle headaches.

You might find the best solution is to use separate pages (using the master for layout), plus url trimming.

So that \search is restricted to authenticated users in web.config. So the unwashed masses get redirected to \Login (which contains the ad) if they attempt to go to search.

brian b
A: 

A few things, maybe something is helpful :)...

  1. I think you may be looking for a a LoginView Class. It will display a separate control or any html based on the authentication status or even roles. Apologies if I am telling you something you already know.

  2. Too replace LoginView functionality, which seems to be what you're describing to me, a Server control would be much more appropriate. Specifically a Composite Server Control.

  3. The dynamically load functionality can be done by adding ASP.Net AJAX to the mix. Your composite control can be based on an ASP.Net AJAX UpdatePanel, for instance. You can also base your server control on ScriptControl base class.

  4. Even if you can load dynamically using AJAX, I would recommend using something that is already done for you. That is, build a Web Part Server Control

kervin