Click here to Skip to main content
13,355,335 members (70,629 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 30 Aug 2006

Dynamic Controls Explained

, 30 Aug 2006
Rate this:
Please Sign up or sign in to vote.
A simple explaination of a sometimes complicated topic

Sample Image - Dynamic_Controls.gif

Sample Image - Dynamic_Controls2.gif


The sample is meant to convey an idea that doesn't get a lot of coverage on the web; creation of dynamic controls and how to make them work with ViewState.


I never had a real need to learn much about dynamic controls until I started working on a project for a client that wanted some complex functionality that was very flexible.  As I tried to find help on how to accomplish my task I quickly realized that there was very little coverage of something so easy to achieve.  After I found the solution I decided to write this article so that others wouldn't have to work as hard as I did when faced with the same problem.

Using the code

This code shows how to add dynamic controls to a page and allow ViewState to do the rest.  The key to getting dynamic controls to work properly is to add the controls in the pages Init event since ViewState gets loaded on the pages Load event.  You must also make sure that the dynamic controls are re-created on every post-back.

/// <span class="code-SummaryComment"><summary></span>
/// Override the page initialize event; this is where your controls should
/// be added.  Dynamic controls need to be re-created on each post-back.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="e"></param></span>
protected override void OnInit(EventArgs e) {

    // My button that will cause the form to post back
    myButton = new Button();
    myButton.Text = "Click Me";
    myButton.Click += new EventHandler(myButton_Click);

    // The drop-down list that will be saved by ViewState
    myDropDown = new DropDownList();
    myDropDown.ID = "drpTest";
    myDropDown.Attributes.Add("runat", "server");
    myDropDown.EnableViewState = true;
    myDropDown.Items.Add(new ListItem("Item 1", "1"));
    myDropDown.Items.Add(new ListItem("Item 2", "2"));
    myDropDown.Items.Add(new ListItem("Item 3", "3"));

    // The table that will be used to organize the look of the controls
    myTable = new Table();

    // Adding the drop-down control to the table
    myRow = new TableRow();
    myCell = new TableCell();
    myCell.HorizontalAlign = HorizontalAlign.Left;

    // Adding the button control to the table
    myRow = new TableRow();
    myCell = new TableCell();
    myCell.HorizontalAlign = HorizontalAlign.Left;

    // Make sure the table control gets added to the page's form
    foreach (object item in Page.Controls) {
        if (item.GetType() == typeof(HtmlForm)) {

This code shows how to retrieve the value of the dynamically create drop-down control upon post-back. You cannot just use the request object to get the selected value from the control.

    /// <span class="code-SummaryComment"><summary>
</span>    /// This method will show the way to retrieve the data selected in the
    /// drop-down list.
    /// <span class="code-SummaryComment"></summary>
</span>    /// <span class="code-SummaryComment"><param name="sender"></param>
</span>    /// <span class="code-SummaryComment"><param name="e"></param>
</span>    protected void myButton_Click(object sender, EventArgs e) {
        Response.Write("Drop-Down List Value: " + ((DropDownList)FindControl("drpTest")).SelectedValue);

Points of Interest

I tried to use this code to create a web control instead of a web page but ran into some problems since setting properties doesn't happen until after the control's load event.  If anyone has an example of how to set property values before the init event I would be interested to see it. 


Project Created - 08/30/2006


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I'm an application developer with over 6 years experience, 4 of those in .Net. I'm currently working in Austin, Texas for a company named Catapult Systems. I'm also taking on clients for a business I've started on the side. I recently moved from using .Net 1.0 to .Net 2.0 and am experiencing all that is .Net 2.0.

You may also be interested in...


Comments and Discussions

Generalhelp Pin
Balihikya5-Apr-07 7:33
memberBalihikya5-Apr-07 7:33 
GeneralUpdate source code Pin
Tancev Sasa14-Jan-07 2:23
memberTancev Sasa14-Jan-07 2:23 
Questionneed help Pin
gs_dhaliwal13-Dec-06 21:15
membergs_dhaliwal13-Dec-06 21:15 
AnswerRe: need help Pin
BTrabon14-Dec-06 3:24
memberBTrabon14-Dec-06 3:24 
GeneralRe: need help Pin
gs_dhaliwal14-Dec-06 20:21
membergs_dhaliwal14-Dec-06 20:21 
GeneralAppreciation Pin
jharri1us9-Nov-06 5:25
memberjharri1us9-Nov-06 5:25 
GeneralRe: Appreciation Pin
BTrabon10-Nov-06 3:20
memberBTrabon10-Nov-06 3:20 
GeneralAny way to optimize Pin
Chanito18-Sep-06 11:59
memberChanito18-Sep-06 11:59 
GeneralRe: Any way to optimize Pin
BTrabon18-Sep-06 15:04
memberBTrabon18-Sep-06 15:04 
GeneralRe: Any way to optimize Pin
Chanito22-Sep-06 3:45
memberChanito22-Sep-06 3:45 
GeneralReg Dynamic controls Pin
kumarcs7-Sep-06 5:24
memberkumarcs7-Sep-06 5:24 
GeneralRe: Reg Dynamic controls Pin
BTrabon7-Sep-06 9:24
memberBTrabon7-Sep-06 9:24 
GeneralRe: Reg Dynamic controls Pin
kumarcs7-Sep-06 19:02
memberkumarcs7-Sep-06 19:02 
GeneralRe: Reg Dynamic controls Pin
BTrabon8-Sep-06 3:26
memberBTrabon8-Sep-06 3:26 
GeneralRe: Reg Dynamic controls Pin
kumarcs8-Sep-06 5:16
memberkumarcs8-Sep-06 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 30 Aug 2006
Article Copyright 2006 by BTrabon
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid