Introduction to ASP.NET Label
Label control is used to display the text on the web page. This Label control is used along with other controls like textboxes. it’s very common where the label is used with a textbox. Labels usually help the user to provide the data in the text boxes by the written indications. Labels are server-side controls. We can display the text in label control via setting the text through text property. By changing its label control properties, we can change the label controls style or look and feel. So we can customize it as per our requirement.
< asp:LabelID="Label1" runat="server" Text="Label" ></asp:Label>
How does ASP.NET Label work?
Let us see how exactly ASP.NET label works.
- Open a visual studio and create a new empty web application.
- Now add a new web form, which is there in the solution explorer.
- From the toolbox, select the label control to drag it and drop it on the web form.
- Set its Text and ID property from the label control.
- We need to assign the text in label control so as to use its text property. by setting the text value in a label text property, the text will be displayed on the label control.
- We can change the text of the label using its text property programmatically via writing code in asp.cs page.
- Text= “EduCba”;
Public partial class _Default : System.Web.UI.Page
Protected void page_Load(object sender, EventArgs e)
Label1.Test=” Welcome to EduCba”;
Label Control has its own properties, which will be used to make it more enhanced.
- AccessKey: To add the keyboard shortcut for the label, Accesskey is used.
- TabIndex: It determines the index of tab control for the webserver.
- BackColor: To enhance the look and make it with different colors, we can use this property so that it will help to change the background color of the label.
- BorderColor: If we want to set the color for the label border, we can use this property.
- BorderWidth: This property will allow us to set a particular width for the label border.
- Font: We need to set the font for the text of the label then we can use this property.
- Forecolor: It is used to set the color for the label text.
- Text: Text, which needs to be displayed for the label, is used by this property.
- ToolTip: It provides the text to be displayed when we try to put the mouse over a label.
- Visible: It will allow us to set the visibility of the control on the web form.
- Height: It provides us to set the height of the label control.
- AutoSize: If we need to resize the label control automatically, it will allow us to set the value for it.
- BorderStyle: We can design the border of the label control as per the application requirement.
- FlatStyle: It deals with the flat style appearance for the label control.
- Font: It will determine the font for the text of label control. It will set the value for it.
- TabStop: If the user can use the tab to the label control, then this property determines the value for it.
- TextAlign: It will provide the alignment for the text in the label control.
Example of ASP.NET Label
Below are the examples of ASP.NET Label:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs"
<form id="form1" runat="server">
<h4>Fill the Employee ID in below text box:</h4>
<asp:Label ID="Label1" runat="server" Text="Employee ID"></asp:Label></td>
<asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style2"></asp:TextBox></td>
<asp:Label ID="Label2" runat="server" Text="Upload a File"></asp:Label></td>
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
Explanation to the above code: We can see in the above code that asp gives its own tag to use it as label.<asp:Label ID =Label1> determines its id. Run at is always set to be “Server” as this is server-side control. We have added 2 labels here having the text as Employee ID and upload file. We can use this Label Control Method.
- GetPreferredSize: It is used to retrieve the rectangular area’s size into which a control can be fitted.
- Refresh: It will immediately redraw itself and any dependent control if any. Also, force to control to invalidate its client area.
- Select: It activates the control.
- Show: It will display the control to the user.
- ToString: It will return the string, which is having the name of the control.
There is some Label control given below:
- AutoSizeChanged: When the value of AutoSize Property changes this occurs.
- Click: When we clicked the Label Control, this is used.
- DoublClick: When label control is double-clicked this occurs.
- GotFocus: When label control receives focus this occurs.
- Leave: When input controls leaves, the focus of the control label it comes under the picture.
We have learned how exactly the labels work in asp.net framework. With the help of an example, it is more clear that how it has been used to display the text in the boxes. We also go through the properties of Labels by which it can be enhanced more.
This is a guide to ASP.NET Label. Here we discuss Syntax, how ASP.NET label works with examples and code implementations. You can also go through our other related articles to learn more –