Library code snippets
Programatically Load User Controls
The other day I was working on a code behind form and I needed to dynamically load a UserControl and access its properties. I was stumped, but with a little help from the codejunkies at aspnextgen.com I was able to move on. Here is a little sample to demonstrate this technique.
This sample consists of 3 pages:
- default.aspx -- the standard ASP.NET page
- cbDefault.vb -- the codebehind page for default.aspx
- ucHeading.ascx -- a usercontrol with a property called page heading.
default.aspx simply has a panel object as a placeholder, and references the code behind file.
default.aspx
<%@ Page EnableSessionState="false" explicit="true" strict="true" MaintainState="false" inherits="cbDefault" src="cbDefault.vb"%>
<html>
<head>
<title></title>
</head>
<body bgcolor="#FFFFFF" >
<asp:panel id="pnlHeading" runat="server" />
</body>
</html>
ucHeading has a single property called PageHeading. PageHeading is a string that is used to display the heading of the page for the web surfer.
ucHeading.ascx
<script runat="server">
Private _pageheading as String = ""
Public Property PageHeading As String
Get
Return _pageheading
End Get
Set
_pageheading = value
End Set
End Property
</script>
<h1><%=_pageheading%></h1>
cdDefault.vb is the code behind file for default.aspx. It contains the meat of this demo. The logic of cbDefault.vb is:
- Load the UserControl
- Get the type of UserControl
- Get access to the property "
PageHeader" - Set the property "
PageHeader" - Add the UserControl to the panel object on
default.aspx
cbDefault.vb
Option Strict On
Option Explicit On
Imports System
Imports System.Reflection
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class cbDefault : Inherits Page
Public pnlHeading as Panel
Public Sub Page_Load(Src As Object, E As EventArgs)
' Load the control
Dim myUC as UserControl = LoadControl("ucHeading.ascx")
' Set the Usercontrol Type
Dim ucType as Type = myUC.GetType()
' Get access to the property
Dim ucPageHeadingProperty as PropertyInfo = ucType.GetProperty("PageHeading")
' Set the property
ucPageHeadingProperty.SetValue(myUC,"Access a Usercontrol from Code Behind",Nothing)
pnlHeading.Controls.Add ( myUC )
End Sub
End Class
Related articles
Related discussion
-
Profile Class does not work after Translation
by converter2009 (1 replies)
-
what is the SQL Server Provider
by hayperaktib (1 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
Java Script, File uploading on ftp server using java script code
by h_c_a_andersen (2 replies)
-
sharepoint calendar web part with events from sql table
by converter2009 (2 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
however define an interface in your app_code directory that simply defines all of the properties that you want in your control, have your control implement this interface then when you load the control you can access the properties like so in the page where the control is loaded:
Control yourcontrol = LoadControl("UI/YourControl.ascx");
((IYourInterface)expander).Property = "some string";
This thread is for discussions of Programatically Load User Controls.