In this tutorial you will work with the list control to create a dropdown list and a checkbox list. We will also add a label control to function with the list to display the selections we made. The code in this tutorial is in C#.

Step 1

Open Visual Studio 2010 or Visual Web Developer 2010 Express and start a New Empty Website. Make sure the Place Code in Separate File box is checked. Switch to Design View and in the toolbox under the standard menu, click and drag a DropDownList Control onto the page. Click to the right of the DropDownList to deselect it and then press “Enter” twice to create a space. In the Toolbox under the standard menu, click and drag a “CheckBoxList” onto the page under the DropDownList.

Click to the right of the CheckBoxList to deselect it, and then press Enter twice to create another space. In the Toolbox, click and drag a Button Control onto the page. Click to the right of the button to deselect it, and then press Enter twice to create another space. In the Toolbox click and drag a Label Control onto the page. The design should look similar to the image below:


I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.

Step 2

In the Smart Tasks Panel of the DropDownList select Edit Items. When the ListItem Collection Editor appears, click Add 3 times to add 3 items to the list. Set the text property and value for each item as “Item1”, “Item2” and “Item3” respectively then click Ok. See the image below.



Switch to Source View and then copy and paste the List items from the DropDownList Control to the CheckBoxList Control. See code below:
Code Block
ListControls.aspx

<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
            <asp:ListItem>Item3</asp:ListItem>
        </asp:DropDownList>
    
        <br />
    
    </div>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
            <asp:ListItem>Item3</asp:ListItem>
    </asp:CheckBoxList>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>

Step 3

Switch back to Design View and double click on the Button Control to create an event handler. In the code behind page, copy and paste the following code into the click event:
Code Block
ListControls.aspx.cs

foreach (ListItem ITemplate in CheckBoxList1.Items)
        {
            if (ITemplate.Selected == true)
            {
                Label1.Text += "In the Check Box List you selected " + ITemplate.Value + "<br />";
            }
        }

The entire code-behind page should appear as below:

Code Block
ListControls.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class ListControls : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "In the Drop Down List you Selected " +
            DropDownList1.SelectedValue + "<br />";
 
        foreach (ListItem ITemplate in CheckBoxList1.Items)
        {
            if (ITemplate.Selected == true)
            {
                Label1.Text += "In the Check Box List you selected " + ITemplate.Value + "<br />";
            }
        }
    }
}

Save your work and then run or debug the application.

When the page loads select an item (1-3) in the DropDownList and then check an item (1-3) in the CheckBoxList. When you click on the button the page refreshes with your selections displayed at the bottom.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!

list_control_source.zip