How to enable disable ListBox programmatically in asp.net c#

Enable Disable ListBox Programmatically
asp.net listbox web server control can be use to create a single or multiple item selection list control. we can set or get the listbox Single or Multiple selection mode using its SelectionMode property. listbox contains ListItem objects. listbox control support data binding, so listbox can be populated from a datasource control.

asp.net developers can enable or disable a listbox control programmatically at run time. listbox Enabled property allow developers to switch between enabled and disabled state of a listbox server control. Enabled property hold a Boolean value. if we set its value True then the listbox control will be enabled and if we set the property value False then the listbox control will be disabled. when the Enabled property value set to False, the listbox control typically appears dimmed in web browser.

the following c# example source code demonstrate us how can we enable or disable a listbox server control dynamically at run time in asp.net.
ListBoxDisable.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ListBox1.Enabled = false;
        Label1.Text = "ListBox Now: Disable";
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        ListBox1.Enabled = true;
        Label1.Text = "ListBox Now: Enable";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to enable disable ListBox programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net ListBox Example: Enable Disable</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Text="asp.net Controls"
             Font-Bold="true"
             ForeColor="BlueViolet"
             Font-Size="Larger"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server"
             Text="asp.net Controls"
             Font-Bold="true"
             ForeColor="SlateBlue"
             >
        </asp:Label>
        <br />
        <asp:ListBox 
             ID="ListBox1"
             runat="server"
             AutoPostBack="false"
             >
             <asp:ListItem>SiteMapPath</asp:ListItem>
             <asp:ListItem>AppearanceEditorPart</asp:ListItem>
             <asp:ListItem>Timer</asp:ListItem>
             <asp:ListItem>TreeView</asp:ListItem>
             <asp:ListItem>PropertyGridEditorPart</asp:ListItem>
        </asp:ListBox>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="Disable ListBox"
             Font-Bold="true"
             ForeColor="SlateBlue"
             OnClick="Button1_Click"
             />
        <asp:Button 
             ID="Button2" 
             runat="server"
             Text="Enable ListBox"
             Font-Bold="true"
             ForeColor="SlateBlue"
             OnClick="Button2_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples