mike mcintyre's

.N e t J o u r n a l

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

SepOctober 2006Nov
SMTWTFS
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

Archives

Topics

Source Code

Friday, October 13, 2006 #

Source Code: Programmatically Load Web User Control At Runtime

How can you programmatically load an ASP.NET 2.0 UserControl at runtime?

First, the UserControl must be loaded using the LoadControl method. You must pass a string, that is the virtual path to the UserControl to be loaded, to the LoadControl method. The LoadControl method returns a control object.

' Declare a variable named controlToLoad of type Control.

Dim controlToLoad As Control

 

' Call the Page LoadControl method passing it the virtual path to

' a ASP.NET UserControl (.ascx file)

' assigning the resulting control object to the controlToLoad variable.

controlToLoad = Me.LoadControl("~\ExampleUserControl.ascx")

 

Next, the control instance must be added to a control collection contained within the page. This could be the page's control collection, or another control's control collection, for example a DIV control.

      ' Add the control object (an instance of the DemoUserControl class)

      ' to the 'bodyRowTwo' DIV contained in this page.

      Me.bodyRowTwo.Controls.Add(controlToLoad)

The Visual Studio 2005 ASP.NET solution source code included with this article demonstrates how to implement the LoadControl method to dynamically load a UserControl at runtime:

1. When the web site's default page is loaded, a single button named 'Select Size' is loaded on the page.

   

2. When a user clicks the 'Select Size' button a user control named "DemoUserControl' is programmatically loaded.

    

Code

Imports System.IO

Partial Class _Default

    Inherits System.Web.UI.Page

 

    Public pathToDemoUserControl = "~\ExampleUserControl.ascx"

 

    Protected Sub selectSizeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles selectSizeButton.Click

 

        ' Clear the DIV where the DemoUserControl is to be loaded.

        Me.bodyRowTwo.Controls.Clear()

 

        ' Call this form's LoadExampleUserControl method.

        Me.LoadExampleUserControl()

 

    End Sub

 

    Protected Sub LoadExampleUserControl()

        Try

 

            ' Declare a variable named controlToLoad of type Control.

            Dim controlToLoad As Control

 

            ' Call the page's LoadControl method passing it the virtual path to

            ' an ASP.NET UserControl (.ascx file), assigning

            ' the resulting control object to the controlToLoad variable.

            controlToLoad = Me.LoadControl(pathToDemoUserControl)

 

            ' Add the control object (an instance of the DemoUserControl class)

            ' to the 'bodyRowTwo' DIV contained in this page's control hierarchy.

            Me.bodyRowTwo.Controls.Add(controlToLoad)

 

        Catch ex As Exception

            ' Message user...

            Me.statusLabel.Text = "Exception: " & ex.Message & _

                "." & vbCr & "Could not load a DemoUserControl."

        End Try

 

    End Sub

 

End Class

Click the link above to download Visual Basic source code in a Visual Studio 2005 web solution that demonstrates how to programmatically load an ASP.NET 2.0 UserControl at runtime.

For more information visit the link below:

How to: Create Instances of ASP.NET User Controls Programmatically

 

Mike McIntyre http://www.getdotnetcode.com

posted @ 6:18 PM