Label Control In ASP.NET

Introduction 

In this article, you will learn how to use Label in ASP.NET Framework and how Label works.

What are Label Controls

  • The label control is used to display text on a website.
  • It is primarily used to generate captions for other controls such as textboxes.
  • Labels typically assist the user in entering data into text boxes by providing written instructions.
  • Labels are controls on the server-side.
  • The Label class can be found in the System.Web.UI.WebControls namespace.

Adding Label Control

  • To create a label, we can either write code or use Visual Studio's drag and drop feature.
  • This is a server-side control, and asp provides its own tag for label creation.

The following is an example and syntax to add label control.

<asp:Label_ID="LabelNew" runat="'server" Text="Label_Control"></asp:Label>

here Runat='Server' Indicates the accessibility of the control at Serverside

Properties of Label Control

Label Control has its own properties that will be used to improve it. (Syntax for each label control property is given in ASP.NET code)

AccessKey

Accesskey is used to add the keyboard shortcut for the label.

<asp:webcontrol id="id" AccessKey="accessKey" runat="server" />

Tablndex

It determines the webserver's tab control index.

<asp:TextBox ID="txtName" runat="server" TabIndex="0"></asp:TextBox>

BackColor

This property is used to change the look and make it with different colours.

<asp:webcontrol id="id" BackColor="color" runat="server" />

BorderColor

We can use this property to change the colour of the label border.

<asp:webcontrol id="id" BorderColor="color" runat="server" />

BorderWidth

This property will allow us to specify the width of the label border.

<asp:webcontrol id="id" BorderWidth="length" runat="server" />

Font

This property will allow us to specify the width of the label border.

<asp:webcontrol id="id" font-subproperty="value" runat="server" />

Forecolor

It's used to change the colour of the label text.

<asp:webcontrol id="id" ForeColor="color" runat="server" />

Text

This property makes use of text that must be displayed for the label.

<asp:HyperLink Text="string" runat="server" />

ToolTip

It specifies the text that will be displayed when we move the mouse over a label.

<asp:webcontrol id="id" ToolTip="string" runat="server" />

Visible

It will allow us to customise the control's visibility on the web form.

<asp:webcontrol id="id" Visible="True|False" runat="server" />

Height

It allows us to customise the height of the label control.

<asp:Image ID="Image1" runat="server" Height="value" />

Width

It allows us to adjust the width of the label control.

<asp:Image ID="Image1" runat="server"  Width="value" />

BorderStyle

The border of the label control can be designed to meet the needs of the application.

<asp:webcontrol id="id" BorderStyle="style" runat="server" />

CssClass

It gives us the CSS class of the label control.

<asp:webcontrol id="id" CssClass="style" runat="server" />

Events for Label Controls 

DataBinding

When the server control binds to a data source, this is referred to as data binding.

Disposed

When an ASP.NET page is requested, a server control is released from memory, which is the final stage of the server control lifecycle.

Init

Occurs during the initialization of the server control, which is the first step in its lifecycle.

Load

When the server control is loaded into the Page object, this event occurs.

PreRender

This occurs after the Control object has been loaded but before rendering.

Unload

This happens when the server control is loaded from memory.

Methods for Label Control

DataBind()

Binds a data source to the server control that was invoked and all of its child controls.

Dispose()

Allows a server control to perform final cleanup before releasing it from memory.

Equals(Object)

Checks whether the specified object is the same as the current object.

Focus()

Sets a control's input focus.

OnLoad(EventArgs)

The Load event is raised.

ToString()

This method returns a string that represents the current object.

Conclusion 

Here, we have learned how label works in ASP.NET Framework and how label controls are useful for us to use.

If you have any queries/suggestions on the article, please leave your questions and thoughts in the comment section below. Follow C# Corner to learn more new and amazing things about ASP.NET or to explore more technologies.

Thanks for reading and I hope you like it.


Similar Articles