asp.net literal control
Literal control is an asp.net server control used to display a static text.
Namespace: System.Web.UI.WebControls
<asp:Literal Text="Testing literal control" runat=server />
Properties of Literal control
Text: It specifies text to display in control.
Mode property to Encode, the Literal control will encode the content of the Text property.
Drag Label control and literal control then write below code in Page_Load event
<asp:Literal ID=”Literal1″ runat=”server”></asp:Literal>
<br />
<asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>
<br />
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = “<SPAN class=\”font\”>My Text Is Blue</SPAN>”;
Label1.Text = “<SPAN class=\”font\”>My Text Is Blue</SPAN>”;
}
Run the application right click on the browser and observe the code for label control and literal control
<SPAN>My Text Is Blue</SPAN> //literal control
<span id="Label1"><SPAN>My Text Is Blue</SPAN></span>
//for label control
Example 2: Getting Today Date function into Literal control
Drag literal control its id is Literal2
Write below todayDate function in Default.aspx.cs Page
public string todayDate()
{
return DateTime.Now.ToShortDateString();
}
Getting todayDate() function date value into literal control
Literal2.Text = todayDate();
Note: Label control will not allow this
Complete code:
protected void Page_Load(object sender, EventArgs e)
{
Literal2.Text = todayDate();
}
public string todayDate()
{
return DateTime.Now.ToShortDateString();
}
SEO for Literal control
Asp.net Literal control can be used as Meta Tag Keywords within the Html Head tags
<head runat=”server”>
<asp:Literal ID=”ltMeta” runat=”server”></asp:Literal>
<head>
protected void Page_Load(object sender, EventArgs e)
{
string metaDBstring=”asp.net,C#,MVC,MVP”;
ltMeta.Text = “<META NAME=’KEYWORDS’ CONTENT=’” +metaDBstring+”‘>”;
}
Note: You can retrieve metaDBstring from Database column
Now Run the application then right click in browser à choose view Page source àyou can html code of the page observer meta tag elements
<head><META NAME='KEYWORDS' CONTENT='asp.net,C#,MVC,MVP'>
<title>
</title>
<head>
Similary for title tag
Design code
<title>
<asp:Literal ID=”ltTitle” runat=”server”></asp:Literal>
</title>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string metaDBstring=”asp.net,C#,MVC,MVP”;
ltMeta.Text = “<META NAME=’KEYWORDS’ CONTENT=’” +metaDBstring+”‘>”;
ltTitle.Text = “Asp.net Overview of Frameworks”;
}
Result in Browser Right Click àview Page source
<title>
Asp.net Overview of Frameworks
</title>
