Disable CheckboxList ListItems for Visual Studio 2003

I needed to disable listitems in a checkboxlist in Visual Studio 2003. Visual Studio 2005 includes a disabled property but Visual Studio 2003 does not. I even tried using reflection to access the hidden setattribute method but that still didn’t have the attribute render to the page.

The following is what I used to accomplish disabled items that are not selected.

  // For Visual Studio 2003. Visual Studio 2005 has a disabled property
  private void SetDisabledCheckBoxItems(CheckBoxList cbl) {
    ArrayList al = new ArrayList();
    for (int i = 0; i < cbl.Items.Count; i++) {
      if (cblProducts.Items[i].Selected == false) {
        al.Add(i.ToString());
      }
    }
    string[] items = al.ToArray(typeof(string)) as string[];
    StringBuilder sb = new StringBuilder();
    sb.Append(“<script language=’javascript’>”);
    sb.Append(” document.onreadystatechange = ReadyStateChange;”);
    sb.Append(” function ReadyStateChange() {“);
    sb.AppendFormat(” var listItems = new Array({0});”string.Join(“,”, items));
    sb.Append(” for (var i = 0; i < listItems.length; i++) {“);
    sb.AppendFormat(” document.getElementById(‘{0}_’ + listItems[i]).disabled = ‘disabled’;”, cbl.ClientID);
    sb.Append(” }”);
    sb.Append(” }”);
    sb.Append(“</script>”);
    Page.RegisterStartupScript(“script”, sb.ToString());
  } 

Comments are closed.