ASP.NET asp:Literal Use

You want to insert text or HTML content directly to your ASPX page. You don't require a container tag in the HTML, but need to add dynamic content and minimize the number of DOM elements in the rendered page. This document contains examples of the asp:Literal control.

Using asp:Literal

Here we use the <asp:Literal> control we have available. This is a good way to render content from HtmlTextWriter or to add a comment to a page. First I will show an example of the asp:Literal tag in the aspx markup, and then I will show how it can be used in C# source code-behind.

--- ASPX markup that uses asp:Literal ---

<ul>
<li><asp:Literal runat="server" ID="FeaturedCount1"/> articles</li>

<li><asp:Literal runat="server" ID="CategoryCount1" />
    <i><asp:Literal runat="server" ID="CategoryText1" /></i>
    articles similar to this one</li>

Setting asp:Literal controls

We can use the code-behind facility in ASP.NET to "render" the above markup to actual concise page HTML. The above code shows a UL (unordered list), and we want the first list item to say "50 articles". The FeaturedCount1 is where we will insert the 50.

--- Code file for ASPX markup (C#) ---

public partial class SiteHome : MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set the bottom article count message.
        FeaturedCount1.Text = SiteStructure.Instance.MapCount.ToString();
        // FeaturedCountSpan1.Text = "50";

        string categoryName = "Category"; // Just for example.
        int countCat = SiteStructure.Instance.CountCategory();

        CategoryCount1.Text = countCat.ToString(); // "5"
        CategoryText1.Text = categoryName; // "Category"
    }
}

Resulting markup

If you look at the C# code above, it references FeaturedCount1, CategoryCount1, and CategoryText1, all of which are <asp:Literal> tags. What we see next is how ASP.NET renders the literal tags into the actual page. You will notice that all the markup disappears.

--- Output HTML for asp:Literal ---

<ul>
<li>50 articles</li>
<li>5
    <i>Category</i>
    articles similar to this one</li>
    </ul>

Description. The asp:Literal tags are entirely replaced. The asp:Literal tags are completely replaced by their Text content they are assigned. This eliminates the need to create dummy spans that will only confuse browsers. This method is very efficient for client browsers to render, and also smaller for your bandwidth. The next section examines this.

Insert HTML comments

I puzzled over the best way to insert an HTML comment directly into the page in ASP.NET. One excellent way is to use an asp:Literal and set its Text to the comment, including the comment start and end blocks. This will create a very nice and clear markup comment. Make an asp:Literal with an ID of Comment1.

// Shows HTML comment in ASP.NET.
Comment1.Text = "<!-- HTML comment with literal. -->";

Summary

Here we saw how you can use asp:Literal for the common problem of inserting dynamic markup, comments, or other text into an HTML page. It is far more logical than Response.Write, and more object-oriented. It will help you keep your code logical and easy-to-maintain. It reduces DOM elements and helps with client and server performance.

See Web Forms Content.

© 2007-2010 Sam Allen. All rights reserved.

Dot Net Perls  Sam Allen