Loading UserControl from code without @reference
Hi,
I would like to use LoadControl (in ASP.NET 2.0) to load a user control as described inhttp://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
Well the
TemplateControl.LoadControl method allows you to load controls without referencing them in the page. It is passed the virtual path to the .ascx file of the UserControl.
Hi David,
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
Can you mandate a base type or interface?
Eh... I'm not sure what you mean by that (maybe due to my knowledge of the English language :-( )
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?
Yes... I would like to set some properties that are specific for each user control. Besides, I would like to use the overload of
that takes the type and a parameter-array. I would rather not use a reference to a file.
If you do not reference the .ascx file you will lose all of the settings stored in the file and none of the underlying controls will be initialized.
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.
Hi, I understand what you are trying to do, and the simple answer is that you can't really do it. The new compile model in ASP .Net 2.0 does not allow you to make references to the user controls directly like you could in ASP .Net 1.1, so you need the @reference to signal the compiler to go ahead and compile the user control and load the assembly so you can reference it. From the 1.1 world, this seems like a silly requirement, as you could declare whatever user controls in your web project you wanted, and the type would be available to your web pages without having to alter each one with a @register page parameter, but it makes sense if you take into account that each web control and page is compiled into its own assembly in ASP 2.0 instead of into the same assembly as it was in 1.1 (or whatever arrangement you chose based on how you organized your project). This is a major architectural difference and affects references, variable scope, and several other features of your page design.
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!
John Bailey wrote: |
| each web control and page is compiled into its own assembly in ASP 2.0 instead of into the same assembly as it was in 1.1 |
|
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