Dynamically Adding Controls
This article will demonstrate how you can design and build flexibility into your ASP.NET pages by adding controls dynamically at runtime.You'll learn to add simple controls to a page, progress to adding a user control into a Placeholder control, and then advance to using multiple Placeholder controls to build a template page that is flexible and easy to use.
Real World Applications
I'm a baseball fan. While I'd rather watch a game in person, more often than not, I'm watching it on the television. Something that you may not consciously notice while watching a game on television is the backstop. You see it every time the camera angle from the pitcher's point of view is displayed. Next time you're watching a baseball game on television, keep your eye on the advertising billboard on the backstop. You will notice that the advertising changes on a regular basis. Physically at the ballpark there is a blue screen located on the backstop and the advertising is inserted real time during the game for the television viewing audience. The players, coaches, and fans at the ballpark never see the advertising.
Even experienced developers may not have found the need to add a control at runtime.
You've got to admire the marketing genius who came up with this idea. I can imagine someone thinking, "If only there was a way I could sell the same billboard space to more than one advertiser. There's got to be a way!"
So why am I mentioning this in an article about dynamically adding controls at runtime to an ASP.NET page? Because inserting different advertising messages throughout the game demonstrates exactly the type of thing you can do by adding controls at runtime.
Why Add Controls at Runtime?
This seems like the logical place to start. For anyone who has already found the need to add controls at runtime in a VB, C++, or other Win32 applications, the question may seem fairly fundamental. If you haven't experienced the need it's a very valid question. Even experienced developers may not have found the need to add a control at runtime.
So, why add controls at runtime? The primary reasons are flexibility and power. Adding controls at runtime provides the flexibility to design user interfaces that can appear and behave differently to different users. Imagine a situation where, depending on the security level of the currently logged on user, certain controls are displayed and others are not. Yes, you could accomplish the same functionality by setting the visible property of the controls but with that solution you're potentially left with unattractive spaces in the UI where the invisible controls are located. An alternative solution would be to dynamically add the controls at runtime that the user is allowed to work with.
This is just one of a million different scenarios that lend themselves very well to take advantage of the flexibility and power of adding controls are runtime.
Basic ASP.NET Page Architecture
Before you can start adding controls you need to make sure you understand a few architectural issues. ASP.NET pages are built from controls. Everything on the page is a control. Labels, textboxes, command buttons, datagrids, static text, and even the HTMLForm, are represented by a control.
The Page.Controls collection contains a reference to every control contained on a page. Private Sub cmdSave_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Dim oControl As Control
For Each oControl In Page.Controls
Private Sub ControlsList(ByVal oPassed As Object)
Dim oControl As Control
For Each oControl In oPassed.Controls
Response.Write(oControl.ID + "<P>")
The above two procedures contain code that loops through the controls on a form and displays the ID property. The cmdSave_Click loops through the Controls collection on the page and passes each control to the ControlsList procedure, which loops through a container control and lists all the controls.
By: Jim Duffy
Jim Duffy is founder and president of TakeNote Technologies, an award-winning training, consulting, and software development company specializing in .NET software developer training and helping clients create business solutions with Microsoft enterprise technologies. Jim’s expertise is with .NET technologies, ASP.NET, SQL Server and Visual FoxPro-to-.NET conversions. He has a BS degree in Computer and Information Systems and over 25 years of programming and training experience. He is an energetic trainer, skilled developer, and has been published in leading developer-oriented publications.
Jim is a Microsoft Regional Director, a Microsoft MVP, an ASPInsider, and is an entertaining and popular speaker at regional user groups and international developer conferences.
You can find additional information about Jim, TakeNote Technologies, links to his blog, as well as a public training class schedule, on-site training information, consulting information, and software development services at www.takenote.com.
Adding controls at runtime provides the flexibility to design user interfaces that can be different things to different users.