How to display none in a Label programmatically in asp.net

asp.net example - label display none
Label is an asp.net web server control that allow us to display text on web page. we can hide label control and it text by using label control's built in property named Visible. Visible property expect a boolean value. if we set the Visible property value to false then it will hide label control from web page.

in this example code we uses core css style to hide label control and its contain text and html elements. we just added a css style with value to label control by its built in property 'Style'. the Style property's add method allow us to add the css style named 'display' and its value 'none' to the label control programmatically at run time. as a result label control display nothing in we page.

the folowing asp.net c# example code demonstrate us how can we hide label control contents (display none) in an asp.net application web page.
label-display-none.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
    
<!DOCTYPE html>
      
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.CssClass = "LabelDisplayNoneStyle";

        //another way to hide label control content.
        //Label1.Style.Add("display","none");
    }  
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <style type="text/css">
        .LabelDisplayNoneStyle {
            display:none;
        }
    </style>      
    <title>asp.net example - label display none</title>
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            asp.net example - label display none
        </h2>      
        <hr width="550" align="left" color="Gainsboro" />      
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Text="sample label to test label content visibility."
            Font-Size="X-Large"
            ForeColor="Green"
            >      
        </asp:Label>      
        <br /><br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="label display none"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>      
    </form>      
</body>      
</html>
More asp.net examples