Search KB Filter article types
Load UserControl With Reflection
Categories: UserControls, UserControls Switching
Tags: Developers, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
Revision: 1
Posted: 18/Nov/2009
Updated: 23/July/2010
Status: Publish
Types: Article

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Load UserControl using reflection

This article explains how to load a UserControl to a Visual WebGui application in run-time from an assembly that is not referenced in your project.The way this is done in .NET is with Reflection.

Code samplesC# Code
 
string strBaseDir = AppDomain.CurrentDomain.BaseDirectory;
string strSlnDir = strBaseDir.Remove(AppDomain.CurrentDomain.BaseDirectory.LastIndexOf(@"\UserControlLoadInRunTime"));
string strPathToAsm = Path.Combine(strBaseDir, @"WebGUIControlLibrary\bin\Debug\WebGUIControlLibrary.dll");
try
{
    Assembly objAsm = Assembly.LoadFile(strPathToAsm);
    object objTemp = objAsm.CreateInstance("WebGUIControlLibrary.UserControl1");
    Gizmox.WebGUI.Forms.UserControl objUserControl = objTemp as Gizmox.WebGUI.Forms.UserControl;
 
    if (objUserControl != null)
    {
        this.Controls.Add(objUserControl);
    }
}
catch(Exception objException)
{
    // do something
}

VB.NET Code

 
Dim strBaseDir As String = AppDomain.CurrentDomain.BaseDirectory
Dim strSlnDir As String = strBaseDir.Remove(AppDomain.CurrentDomain.BaseDirectory.LastIndexOf("\UserControlLoadInRunTime"))
Dim strPathToAsm As String = Path.Combine(strBaseDir, "WebGUIControlLibrary\bin\Debug\WebGUIControlLibrary.dll")
Try
    Dim objAsm As Assembly = Assembly.LoadFile(strPathToAsm)
    Dim objTemp As Object = objAsm.CreateInstance("WebGUIControlLibrary.UserControl1")
    Dim objUserControl As Gizmox.WebGUI.Forms.UserControl = TryCast(objTemp, Gizmox.WebGUI.Forms.UserControl)
 
    If objUserControl IsNot Nothing Then
        Me.Controls.Add(objUserControl)
    End If
Catch objException As Exception
    ' do something
End Try

The demo application

The demo application is supplied both in C# and VB.NET code for Visual Studio 2005 and 2008 and can be downloaded from here

About the author

Related Articles

UserControls  
Title Update Author
This video demonstrates how to work with user controls.
Tags: Developers, Drag & Drop, 1. Beginner, 2. Intermediate, 3. Advanced, Customization, Layouting, Pre v6.3, v6.3, v6.4 and Later
01/Jan/2009    2009/01/01
This sample application was created on VWG v6.3.x. The creator of this application suggests that VWG v6.4 would give much greater flexibility.
Tags: Architects, Developers, Events, Resource Handlers, VB.NET, 2. Intermediate, 3. Advanced, Customization, Layouting, Pre v6.3, v6.3
07/Jan/2009    2009/01/07
Tags: Architects, Developers, Data Binding, 2. Intermediate, 3. Advanced, Data Binding, Pre v6.3, v6.3, v6.4 and Later
02/Jan/2009    2009/01/02
This sample code demonstrates working with user controls to simulate frames
Tags: Developers, Navigation, 1. Beginner, 2. Intermediate, Navigation, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
12/Jan/2007    2007/01/12
Tags: Developers, Navigation, 1. Beginner, 2. Intermediate, Navigation, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
08/Aug/2010    2010/08/08
Tags: Developers, Navigation, 1. Beginner, 2. Intermediate, Navigation, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
23/July/2010    2010/07/23
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS