Skip Navigation Links.

ASP.NET 2.0 Dynamic CheckBoxList

The following shows an example of dynamically building a CheckBoxList:


You fav team is:

The code is simply added at the Page Load, and the CheckBoxList nodes are deleted, and then added. In this case the links are generated from an ArrayList (links):

if (!IsPostBack) 
foreach (string s in CheckBoxList1.Items) { // get rid of the current nodes CheckBoxList1.Items.Remove(s); } ArrayList links = new ArrayList(); links.Add("Celtic"); links.Add("Hearts"); links.Add("Hibs"); links.Add("Rangers"); foreach (string s in links) { this.CheckBoxList1.Items.Add(s); } }

  and the Event on the checkBox becomes:

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) 
   this.TextBox1.Text = ""; 
   foreach (ListItem li in this.CheckBoxList1.Items)                 
     if (li.Selected) this.TextBox1.Text += li.Text + " ";