Click here to Skip to main content
Click here to Skip to main content

ASP.NET C# Disable/Enable ListItems

, 26 Aug 2004
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how it is possible to disable/enable individual ListItems in a ASP.NET CheckBoxList Server Control.

A Brief Introduction

Have you ever wanted to disable/enable individual list items in a ListControl Server Control in ASP.NET? You might have tried to add attributes to the individual ListItems in a Server Control such as a CheckBoxList or DropDownList? It's impossible and a known bug. I needed to implement a way to disable/enable individual items of a CheckBoxList when a certain item was checked/unchecked. The problem was solved by adding a JavaScript function that is invoked by the onclick event of the CheckBoxList.

Instructions

  1. Add a CheckBoxList to your ASP.NET WebForm and give it an ID of checkBoxListTest.
  2. Add a call to the LoadCheckBoxList in the Page_Load event.
  3. Add the LoadCheckBoxList method to your webpage class.
  4. Add the JavaScript function inside the head of the HTML.

ASP.NET CodeBehind

The following method adds the onclick function disableListItems to the CheckBoxList attributes collection. The function will disable all the items except for the last item in the list when it is checked. The method also adds three items to the CheckBoxList.

private void Page_Load(object sender, System.EventArgs e)
{
    if(!this.IsPostBack)
    {
        LoadCheckBoxList();
    }
}
                    
private void LoadCheckBoxList()
{
    this.checkBoxListTest.Attributes.Add("onclick", 
       "disableListItems('checkBoxListTest', '2', '3')");
    
    // Add three items to the CheckBoxList.
    for(int i=0; i < 3; i++)
    {
        ListItem item = new ListItem(i.ToString(), i.ToString());
        this.checkBoxListTest.Items.Add(item);
    }
}

The following code is the JavaScript function that is invoked by the onclick event of the CheckBoxList.

JavaScript Function

/*******************************************************************
This is the javascript function that is invoked when the checkboxlist
is clicked.

Function:    disableListItems.
Inputs:        checkBoxListId - The id of the checkbox list.

            checkBoxIndex - The index of the checkbox to verify.
            i.e If the 4th checkbox is clicked and 
            you want the other checkboxes to be
            disabled the index would be 3.
            
            numOfItems - The number of checkboxes in the list.
Purpose:  Disables all the checkboxes when the checkbox to verify is
            checked.  The checkbox to verify is never disabled.
********************************************************************/
function disableListItems(checkBoxListId, checkBoxIndex, numOfItems)
{
    // Get the checkboxlist object.
    objCtrl = document.getElementById(checkBoxListId);
    
    // Does the checkboxlist not exist?
    if(objCtrl == null)
    {
        return;
    }

    var i = 0;
    var objItem = null;
    // Get the checkbox to verify.
    var objItemChecked = 
       document.getElementById(checkBoxListId + '_' + checkBoxIndex);

    // Does the individual checkbox exist?
    if(objItemChecked == null)
    {
        return;
    }

    // Is the checkbox to verify checked?
    var isChecked = objItemChecked.checked;
    
    // Loop through the checkboxes in the list.
    for(i = 0; i < numOfItems; i++)
    {
        objItem = document.getElementById(checkBoxListId + '_' + i);

        if(objItem == null)
        {
            continue;
        }

        // If i does not equal the checkbox that is never to be disabled.
        if(i != checkBoxIndex)
        {
            // Disable/Enable the checkbox.
            objItem.disabled = isChecked;
            // Should the checkbox be disabled?
            if(isChecked)
            {
                // Uncheck the checkbox.
                objItem.checked = false;
            }
        }
    }
}

Conclusion

The above code shows that it is possible to enable/disable individual items within a ListBox Server Control in ASP.NET. You can modify the C# and JavaScript code to meet your needs.

License

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

Share

About the Author

Kenny Young

United States United States
Ken currently works as the Director of Software Engineering at the Pediatrics Epidemiology Center at the University of South Florida. He is involved in the architecture and design on numerous clinical trial projects. Some of the project include:
 
1. Rare Diseases Clinical Research Network
2. TrialNet - Diabetes
3. Teddy - Diabetes
4. CCOP - Cancer
5. AIDA - Diabetes

Comments and Discussions

 
GeneralMy vote of 1 Pinmembereyali.k3-Sep-09 2:01 
GeneralThe same technique applied to a RadioButtonList doesn't work ?? [modified] PinmemberAlexandru Matei9-Jun-08 4:00 
GeneralASP.NET C# Disable/Enable ListItems PinmemberGeeki14-Nov-06 17:58 
GeneralRe: ASP.NET C# Disable/Enable ListItems Pinmemberjeff_tu24-May-07 9:28 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 27 Aug 2004
Article Copyright 2004 by Kenny Young
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid