Introduction Of Literal Control In ASP.NET


Literal Control (Class)

Namespace Name :- System.Web.UI
Assembly Name :- System.Web

Literal control is like Label control also used to display text on the web page.
It is also passive control.
But Label control has additional properties than Literal control.

List of some Literal control proprety

1) Text :- The Literal control display the text(string) that is set in the Text property.
2) Id :- Represent the unique id for the Literal Control. Further we can access the text of Literal Control with the help of id.(mandatory)
3) Runat :- Enable the control to run at server side. (mandatory)
4) Mode :- Determines whether the text is transformed or encoded.

It support 3 values :
a) PassThrough   b) Encode   c) Transform

a) PassThrough :- When we set the PassThrough mode for the Literal control the content of the code is display without encoding.
For example, if the Text property of a Literal control contains an <b>Name</b>  then the result  is  Name, regardless the target device or browser supported it or not.

b) Encode :- When we set the Encode mode for the Literal control the content of the code is display after encoding.
For example, if the Text property of a Literal control contains an <b>Name</b>
then the result of comes in the <b>Name</b>, Because it is encode as &lt;b&gt;

c) Transform :- When we set the Transform mode for the Literal control the content of the code is display without encoding, but dependent on the target device or browser, whether it is support that tag or not.
For example, if the Text property of a Literal control contains an <b>Name</b> then the result of comes is Name, if the target device or browser support the <b> tag.
                              Otherwise the <b> tag is skip or the output is Name.



NOTE :- When we place the Literal Control on the Web Page in ASP.NET, there is no rendered tag as in the Label control (render tag is span),means the text of the literal control is just written in the plain text.

========================================================================
Create a Web Form and Named it "LiteralControlDemo.aspx"

========================================================================

Write the following code in the Source part of Web form

========================================================================



<%@ PageLanguage="C#"AutoEventWireup="true"CodeFile="LiteralControlDemo.aspx.cs"Inherits="LiteralControlDemo"%>

<!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>
    Literal control with Mode :- PassThrough &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Literal ID="Literal1"
                     runat="server"
                     Mode="PassThrough"
                     Text="<b>Hi, I am a literal control</b>">
         </asp:Literal>
   
    <br />

    Literal control with Mode :- Encode &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Literal ID="Literal2"
                     runat="server"
                     Mode="Encode"
                     Text="<b>Hi, I am a literal control</b>">
         </asp:Literal>

    <br />
   
    Literal control with Mode :- Transform &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     
    <asp:Literal ID="Literal3"
                     runat="server"
                     Mode="Transform"
                     Text="<b>Hi, I am a literal control</b>">
         </asp:Literal>
       
    </div>
    </form>
</body>
</html>

 ========================================================================
OUTPUT 


 ========================================================================



 ========================================================================
NO RENDERED TAG FOR 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><title>

</title></head>
<body>
    <form method="post" action="LiteralControlDemo.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUHMTc3NTA0NGRkg2WdFr0t2pyCpZFs56p2zg0hozl81XesAGxsWryP7Jg=" />
</div>

    <div>
    Literal control with Mode :- PassThrough &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <b>Hi, I am a literal control</b>
   
    <br />

    Literal control with Mode :- Encode &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &lt;b&gt;Hi, I am a literal control&lt;/b&gt;

    <br />
   
    Literal control with Mode :- Transform &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
    <b>Hi, I am a literal control</b>
       
    </div>
    </form>
</body>
</html>








Introduction Of Literal Control In ASP.NET Introduction Of Literal Control In ASP.NET Reviewed by Baljeet Singh on 00:04 Rating: 5

No comments:

Powered by Blogger.