How to disable specific items in a ListBox in asp.net

asp.net listbox disable item
The following asp.net c# example code demonstrate us how can we disable specific item or items from ListBox server controls programmatically at run time.

ListBox class has no built in property or method to disable specific list item. But we can enable or disable ListBox control itself. To disable specified items from ListBox we need to take help from core CSS style. We can add any valid CSS property to ListBox control item's by attaching them with ListItem object's Attributes collection.

We simply added the CSS disable property with value to ListItem object's Attributes collection to disable the specified items from ListBox control.

First, we search through the ListBox items collection for specific items to disable then we loop through the searched items and convert them to disabled state. The bottom images shows the output of this example code section.
listbox-disable-item.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>    
      
<!DOCTYPE html>
        
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    { 
        if(!Page.IsPostBack)
        {
            string[] birds = { 
                                 "Noisy Miner",
                                 "Mimic Honeyeater",
                                 "Yellow-throated Miner",
                                 "Noisy Friarbird",
                                 "Spiny-cheeked Honeyeater",
                                 "Red Wattlebird"
                             };

            ListBox1.DataSource = birds;
            ListBox1.DataBind();

            ListBox1.Rows = ListBox1.Items.Count;
        }
        //find items from listbox which contains 'Miner'
        var items = from ListItem li in ListBox1.Items
                    where li.Text.Contains("Miner")
                    select li;

        //programmatically disable specific items 
        foreach (ListItem li in items)
        {
            li.Attributes.Add("disabled", "disabled");
        }
    }  
</script>        
        
<html xmlns="http://www.w3.org/1999/xhtml">        
<head id="Head1" runat="server">        
    <title>asp.net listbox disable item</title>  
</head>        
<body>        
    <form id="form1" runat="server">        
    <div>        
        <h2 style="color:MidnightBlue; font-style:italic;">        
            asp.net example - listbox disable item
        </h2>        
        <hr width="550" align="left" color="Gainsboro" />        
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Text="select item(s) from listbox."
            Font-Size="X-Large"
            Width="350"
            >      
        </asp:Label>      
        <br /><br />  
        <asp:ListBox  
            ID="ListBox1"  
            runat="server"  
            AutoPostBack="true"  
            Font-Size="X-Large"
            SelectionMode="Multiple"
            Font-Names="Comic Sans MS"
            >  
        </asp:ListBox> 
    </div>        
    </form>        
</body>        
</html>
More asp.net examples