Articles → .NET → Difference Between Label And Literal Control In Asp.Net
Difference Between Label And Literal Control In Asp.Net
Difference
Literal Control | Label Control |
---|
Primarily used to display static or dynamic text on a web page without adding additional HTML markup. | Used to display text on a web page, typically for labeling other controls. |
Renders text directly into the page output without wrapping it in additional tags (like span) | Wraps the content in an HTML span tag by default. |
Does not support AssociatedControlID property. | Can include additional properties like AssociatedControlID (to link the label with an input control for accessibility). |
Cannot apply styles directly. | Can apply CSS styles or attributes. |
Label control is inherited from WebControl class | Literal control is inherited from Control class. |
Example
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Label_Literal_demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Literal Demo:
<asp:Literal ID="ltrlControl" runat="server" Text="<a href='http://gyansangrah.com'>Click Me</a>"></asp:Literal>
<br />
Label Demo:
<asp:Label ID="lblControl" runat="server" Text="This is the test label"></asp:Label>
</form>
</body>
</html>
Output