ASP.NET Label Control


The Label control used to display text in a set location on a Web page, also it can customize the displayed text through the Text property.

C# Code

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Welcome to ASP.NET Tutorial";
        }
    }
}	

HTML Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="blankpage.aspx.cs" Inherits="asptutorial.blankpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> ASP.NET </title>
</head>
<body>
<form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Welcome to ASP.NET Tutorial">
    
    </div>
    </form>
</body>
</html>

Properties

PropertyDescription
runatSpecifies that the control is a server control. Must be set to "server"
TextThe text to display in the label

ASP.NET Control Standard Properties

AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
For a full description, go to Web Control Standard Attributes.

ASP.NET Web Control Standard Properties

AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
For a full description, go to Web Control Standard Attributes.

Example of Label Control

Declare one Label control, one TextBox control, and one Button control in an .aspx file. When the user clicks on the button, the submit subroutine is executed. The subroutine copies the content of the TextBox control to the Label control.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Write some text";
            Button1.Text = "Copy text";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label2.Text = TextBox1.Text;
        }
    }
}	

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> ASP.NET </title>
</head>
<body>
<form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Welcome to ASP.NET Tutorial">
        <br/>
        <asp:TextBox ID="TextBox1" runat="server">
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label">
    
    </div>
    </form>
</body>
</html>

Output


Share this article on