Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday 18 January 2016

SIMILARITY AND DIFFERENCE BETWEEN LABEL AND LITERAL CONTROL


Today in Engineer's World, we are discussing a topic of C# in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR  +ASP.NET SOLUTIONS

Click imagination hunt to see latest Blogs


SIMILARITY AND DIFFERENCE BETWEEN LABEL AND LITERAL CONTROL
  
D5. To encode text in Label control use Server.HtmlEncode() but in Literal use the mode property.
For ex- 

Practical Implementation: -

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Literal_control.aspx.cs"
    Inherits="Literal_control" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%-- Server.Encode is used to encode string in Label control --%>
        <asp:Label ID="Label1" runat="server">
        <%=Server.HtmlEncode("<script>alert('Label Encode: Danger in code')</script>") %>
        </asp:Label>
        <br />
        <br />
        <%-- mode = Encode is used to encode string in Literal control --%>
        <asp:Literal ID="Literal1" runat="server" Mode="Encode" Text="<script>alert('Danger in code')</script>">
        </asp:Literal>
    </div>
    </form>
</body>
</html>

D6. The hierarchy of both controls is also a little bit different.

Label
Literal
(i) System.Object
(i) System.Object
(ii) Control
(ii) Control
(iii) WebControl
(iii) Literal
(iv) Label


Click imagination hunt to see latest Blogs

For ex-
You can check the flow of control as
Step-1 Go to the code behind of WebForm.
Step-2 In the page load write Label.

Practical Implementation: -

using System;
using System.Web.UI.WebControls;

public partial class Literal_control : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label Label2 = new Label();
        Literal Literal2 = new Literal();
    }

  
Step-3 Right-click on the Label and Go to Definition. You can see the Label is inheriting from the WebControl.
Step-4 If you Right-click the WebControl you can see that it is inheriting from Control.
Step-5 If you Right-click the Control you can see that it is inheriting from the various thing which is directly or indirectly part of System.Object.

For any query, comment us below.

 
Related Questions: -


Q-1 How can a Label control differ than a Literal control?
Ans- Label control's final HTML code has an <span>…</span> HTML tag whereas, Literal control's final HTML code contains only text, which is not surrounded by any HTML tag.

Click imagination hunt to see latest Blogs

Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...