Hide and visible ListBox programmatically in asp.net c#

Hide and visible ListBox programmatically
The following asp.net c# example code demonstrate us how can we programmatically hide a ListBox web server control's from web browser and how can we show (visible) a hidden ListBox control in web browser.

ListBox Class has a built in property to manage ListBox server control's visibility. ListBox 'Visible' property accept a Boolean value. If we set this property value to true then the ListBox control will be render in web browser, and when we set this Visible property value to false it will hide ListBox control from web browser.

To provide exceptional user interface, sometimes .net developers want to completely hide a ListBox control from web browser without disabling it. ListBox class Visible property help developers to change this control's visibility programmatically at run time. We also can set this Visible property value by declarative syntax in tag area.
ListBoxVisible.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        ListBox1.Visible = false;
        Label1.Text = "ListBox Hide.";
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        ListBox1.Visible = true;
        Label1.Text = "ListBox visible.";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to hide, visible ListBox programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Maroon">asp.net ListBox Example: Hide, Visible</h2>
        <asp:Label 
             ID="Label1"
             runat="server"
             ForeColor="SeaGreen"
             Font-Bold="true"
             Font-Size="X-Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server"
             Text="asp.net Controls"
             Font-Bold="true"
             ForeColor="Tomato"
             >
        </asp:Label>
        <br />
        <asp:ListBox 
             ID="ListBox1"
             runat="server"
             AutoPostBack="false"
             >
             <asp:ListItem>RequiredFieldValidator</asp:ListItem>
             <asp:ListItem>ObjectDataSource</asp:ListItem>
             <asp:ListItem>WebPartManager</asp:ListItem>
             <asp:ListItem>WebPartZone</asp:ListItem>
             <asp:ListItem>ValidationSummary</asp:ListItem>
        </asp:ListBox>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             Text="ListBox Hide"
             Font-Bold="true"
             ForeColor="Tomato"
             OnClick="Button1_Click"
             />
        <asp:Button 
             ID="Button2" 
             runat="server"
             Text="ListBox Visible"
             Font-Bold="true"
             ForeColor="Tomato"
             OnClick="Button2_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples