ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
The Top 10 ASP Links @
What is and Why Do I Need It?
An Overview of ASP.NET

Use the Literal Control for Precise Rendering
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Dynamic Form Elements with .NET (C#)

by Matt Rutledge

I assume by now, unless you are just learning .NET, that everyone is familiar with the standard Web Form Elements. Radio buttons, radio button lists, check boxes, check box lists, text boxes, etc. What this article aims to do is present a solution for generating dynamic form elements and retrieving there values. Some of you may be asking what is the reason for this? Well, lets say you have several check box lists that you need to generate from a database, but you don't know how many there are going to be. In this case you need to dynamically generate form elements from a database. You would normally do this in the Page_Load function like this:

    CheckBoxList chkList1 = new CheckBoxList();
    chkList1.ID = "SomeList";
    // populate the check box list
    for(int i = 0; i < nElements; i++)
        ListItem listItem = new ListItem();
        listItem.Value = sNameValue[0];
        listItem.Text = sNameValue[1];
    // Do some other stuff and add the check box list to the page

Now that you have dynamically generated your check box list and you have a pretty form displayed, how do you get the information that the user entered out when they click submit? Since you generated the check box list dynamically you cant just reference the name. You could use Request.Form but this gets a bit messy and if you don't know the exact name then your screwed.

How about this: do basically the same thing you did in the Page_Load function but this time find the check box list control in your page. Like this:

    Private void btnSubmit_Click(...)
        CheckBoxList chkList1 = (CheckBoxList)this.FindControl("SomeList");
        for(int i = 0; i < chkBoxList1.Items.Count; i++)
            if(chkBoxList1.Items[i].Selected == true)
                // Do something with the value
                blah = chkList1.Items[i].Value;

You can do this with any control. This comes in handy when you want to build forms from a database, or any data source for that matter. When you don't know how many elements you will need have no fear, there is a way to get the information out J

If you have any questions please feel free to email me at

Matt Rutledge
Lead Developer
DATA, Incorporated
1777 S. Bellaire Street, Suite Ground Zero
Denver, CO  80222
303.708.8709 (Fax)

DATA is a specialist in digital visual media: 3D-computer animation, multimedia design and development (CD-ROM and DVD), Web development and design with full e-commerce applications, database programming, on-line training, hosting, and complete video production and streaming.

Related Information

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers