Hi,
I would like to use LoadControl (in ASP.NET 2.0) to load a user control as described in http://msdn2.microsoft.com/library/c0az2h86(en-us,vs.80).aspx, without using the @Reference-directive. Is that possible and how
This is because I'm working on a generic framework with a class that describes the flow, loads the usercontrols and sets their properties. So I don't want to use @reference-directives in my aspx-files.
Please help! I'm stuck here!!
Thanx
Bart

Loading UserControl from code without @reference
MadisE
Walter Fuchs
Jon Turlington
Can you mandate a base type or interface
JamesFM
I guess I'm not really sure exactly what you are trying to do. If you are trying to initialize controls without using the .ascx files you might need to use server controls instead of user controls. If you want to set properties of a control without knowing what kind of control you have then there are multiple options.
You could declare a base control class and have all of the controls derive from it. You could put generic code to set properties in the base class, but that is impossible when using third party controls or the regular ASP.NET controls.
You could declare an interface then create derived controls of any controls you want to use and have the derived version of the control implement your interface. That will work with most controls but it will be a lot of copying and pasting. Some third-party contols are marked as sealed which would prevent you from creating a derived type from them.
The other option I see is to use reflection to look into the control you create and set anything you want. This would be the most complicated to write and the most difficult to test.
punk
Thanx for your respons.
I noticed that it should work that way, but I don't have the types available so I can't cast. To make sure you understand what I mean:
What I would like to do is something like this:
MyControl control = (MyControl) TemplateControl.LoadControl(...);
I can't do that, because the type 'MyControl' is not known in that class. The only way I found to do that was to use the @reference-directive and that is exactly what I do not want.
Hope you can help me further....
Thanx,
Bart
Wriju
I didn't know that.... that really is too bad for me, but now I can understand what is happening. Thanx for your complete and accurate reply
danpartee
This is true even if you are loading the control dynamically using the LoadControl method. Since there is no way to know what assembly the user control will actually be created in, reflection does not really apply here (reflection would require that you specify the assembly as well as the type to dynamically load the assembly).
You may try creating a web user control base class, declared in your own assembly, and referencing this assembly from your web project or loading this assembly using reflection. In this way, you can use load control, but cast to the base type, and call the overloaded functionality through your base type. This isn't as straightforward as in 1.1, but it does solve the problem.
It seems silly that something so common in ASP .Net 1.1 is not an option in ASP .Net 2.0, but the change in architecture has this unfortunate effect.
Just be happy you aren't using any "friend" code structures!
valimar
Yes... I would like to set some properties that are specific for each user control. Besides, I would like to use the overload of
Page.LoadControl
plucky
What do you need to do to the user control If you only need to add it to the page then something like this might suffice:
public static UserControl LoadControl(Page page,string location)
{
Control control = page.LoadControl(location);
page.Controls.Add(control);
return control;
}
Is other functionality required