-->

22 December 2019

Asp.Net Label Control

  Asp.Net CS By Example       22 December 2019

  The Label Control :
     In this post,we are leaning about ASP.NET Label Control.The Label control help to display information on web site.When we need to modify the text displayed in a page dynamically, we can use the Label control.


Syntax:
<asp:Label ID="Label1" runat="server" Text="Label" />


We can set text to label control by 2 ways.
1)1st way We can set string value to text property.
e.g. <asp:Label ID="Label1" runat="server" Text="Welcome to Asp.Net" />

2) 2nd way we can place we can place the text between the Label control’s opening and closing tags
e.g. <asp:Label ID="Label1" runat="server" > Welcome to Asp.Net </asp:Label>


  By default, a Label control renders its contents in an HTML tag. Whatever value we assign to the Text property
is rendered to the browser enclosed in a <span> tag.</span>

The Label control supports several properties we can use to format the text displayed by the Label:
Sr.No Property Description
1) ID Set the uniqe value to control to access in code-behind code.
2) Text The error message displayed when validation fails.
3) AssociatedControlID When click on label focus is automatically set in associte textfield control.
4) BackColor change the background color of the label.
5) BorderColor set the color of a border rendered around the label.
6) BorderStyle display a border around the label. Possible values are NotSet, None, Dotted, Dashed,
Solid, Double, Groove, Ridge, Inset, and Outset.
7) BorderWidth set the size of a border rendered around the label.
8) CssClass Associate a Cascading Style Sheet class with the label.
9) Font Set the label’s font properties.
10) ForeColor Set the color of the content rendered by the label.
11) Style Assign style attributes to the label.
12) ToolTip Set a label’s title attribute.

  • In general, we avoid using the formatting properties and take advantage of Cascading Style Sheets to format
    the rendered output of the Label control.
  • When we set AssociatedControlID property of label control then the Label control is rendered as an
    HTML <label> tag instead of an HTML <span> tag.
  • Code: LabalControlDemo.aspx

     <%@ Page Language="C#"%>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <style type="text/css">
            .labelStyle
            {
                color: red;
                background-color: yellow;
                border: Solid 2px Red;
            }
            
            .column
            {           
                width: 45%;
                margin-left: 10px;
                background-color: white;
                border: solid 1px black;
                padding: 10px;
                border-radius: 25px;
            }
            fieldset
            {
                background-color: #36f1d7;
                border: solid 2px #d81a72;
                border-radius: 20px;
                color: #4328b1;
                font-size: 20px;
                padding: 10px;                   
            }
        </style>
        <title>Label Control Demo</title>
    </head>
    <body>
        <form id="form1" runat="server">
    
        <div class="column">
        <fieldset>
                <legend>Label Control with properties and css </legend>
            <asp:Label ID="lblFirst" Text="First Label set Properties" ForeColor="Red" BackColor="Yellow" 
                BorderStyle="Solid" BorderWidth="2" BorderColor="red" runat="server" />
            <br />
            <br />
            <asp:Label ID="lblSecond" Text="Second Label Set css" CssClass="labelStyle" runat="server" />
            </fieldset>
        </div>
    
        <div class="column">
            <fieldset>
                <legend>Label Control with AssociatedControlID property </legend>
                <asp:Label  ID="lblName"  Width="100"  AssociatedControlID="txtName"  runat="server"> 
            Name:</asp:Label>
                <asp:TextBox  ID="txtName"  MaxLength="20"  runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator  ID="rfvName"  ControlToValidate="txtName"  Text="Enter Name"
                     EnableClientScript="true"  SetFocusOnError="true"  runat="server">
                        </asp:RequiredFieldValidator>
                <br />
                <br />
                <asp:Button  runat="server"  ID="btnValidate"  Text="Validate" />
            </fieldset>
        </div>
        </form>
    </body>
    </html>
    
    

    Output:

    logoblog

    Thanks for reading Asp.Net Label Control

    Previous
    « Prev Post