20

What is a LiteralControl? I was reading about LiteralContols but I am not sure why they are used. I used this code to list the controls in a page and - I have a page which has a label and a button. When I use this

foreach (Control control in Page.Controls)
{
    Response.Write(control.GetType().ToString() + " - <b> " + control.ID + "</b><br />");    

    if (control is LiteralControl)
    {
        Response.Write("Textt :" + ((LiteralControl)control).Text.ToString() + " - " + 
            Server.HtmlEncode(((LiteralControl)control).Text + "<br />") ); 
    }
}

I find that there are actually LiteralControls that are generated and listed before and after every control I have added to my page. Literal Controls also have only text property.

I am still not sure why LiteralControls are needed and used? Why are LiteralControls used? Why do controls that I add to my page have one LiteralControl placed before and after? And why do they have only Text Property?

darcyy
  • 5,236
  • 5
  • 28
  • 41
pavanred
  • 12,717
  • 14
  • 53
  • 59
  • To be precise, they also accept *ID* and *runat="server"*. –  Jul 18 '10 at 04:52
  • See [literal-control-in-Asp-Net/](https://www.c-sharpcorner.com/UploadFile/puranindia/literal-control-in-Asp-Net/) – stomy Sep 06 '18 at 14:32

7 Answers7

18

A LiteralControl is used to inject HTML into your page at runtime.

Sky Sanders
  • 36,396
  • 8
  • 69
  • 90
15

From MSDN Literal Control represents HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server.

Also have a look at this for Literal Control usage

A label renders a <span> tag. It allows you to programmatically change the display characteristics of some text you want to display. A LiteralControl renders exactly whatever static HTML you want it to. There is no general advantage of using one over the other. One may be more useful than another in certain circumstances. For example, if you want to display static text, and make no programmatic changes to it, you might want to use a LiteralControl.

ACP
  • 34,682
  • 100
  • 231
  • 371
  • 9
    builderau.com.au link dead. – JerryOL Jun 09 '13 at 16:36
  • Here is an archived builderau.com.au link of the [Literal Control usage](http://web.archive.org/web/20110818214153/http://www.builderau.com.au/program/asp/soa/Use-ASP-NET-s-Literal-control-to-its-full-potential/0,339028371,339286810,00.htm) – stomy Sep 06 '18 at 14:27
5

A lot of programmers who don't understand the value of semantic markup (especially those with primarily a Windows Forms background) will use a Label control or the HTML LABEL tag as a placeholder for programmatically-generated text on a page. The disadvantage to this is that Label has semantic meaning in an HTML document.

Using a literal gives you a control with an ID attribute to hook to, while allowing you to inject semantically correct markup around it.

Additionally, if you end up not pushing any text to a LABEL tag, it will still output the tag in your HTML, like so: <label></label>, whereas a Literal with no text will output nothing - much cleaner.

3

You will get a LiteralControl for any HTML markup that is not part of an ASP.NET server control. It's just how the framework renders the HTML shrubbery of your page. The Text property is just this HTML, which also includes any whitespace.

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
0

Try this:

foreach (Control control in Page.Controls)
{
    Response.Write(control.GetType().ToString() + " - <b> " + control.ID + "</b><br />");    

    if (control is LiteralControl)
    {
        Response.Write("Textt :" + ((LiteralControl)control).Text.ToString() + " - " + 
        Server.HtmlEncode(((LiteralControl)control).Text + "<br />") ); 
    }
}
AmanVirdi
  • 1,667
  • 2
  • 22
  • 32
0

What I did in one web application was to hook into defined literal controls on the page to add text at runtime. A literal control creates no markup of its own, but will happily render any HTML that you provide to the Text property, just as well as raw text.

Grant Palin
  • 4,546
  • 3
  • 36
  • 55
-1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  { }
  protected void btnGenerateControl_Click(object sender, EventArgs e)
  {
     // Retrieve the count of the controls to generate
    int Count = Convert.ToInt16(txtControlsCount.Text);
    // Loop thru each list item in the CheckBoxList
    foreach (ListItem li in chkBoxListControlType.Items)
    {
        if (li.Selected)
        {
            // Generate Lable Controls
            if (li.Value == "Label")
            {
                for (int i = 1; i <= Count; i++)
                {
                    Label lbl = new Label();
                    lbl.Text = "Label - " + i.ToString() + "<br>";
                    //phLabels.Controls.Add(lbl);
                    //tdLabels.Controls.Add(lbl);
                    pnlLabels.Controls.Add(lbl);
                }
            }
            // Generate TextBox controls
            else if (li.Value == "TextBox")
            {
                for (int i = 1; i <= Count; i++)
                {
                    TextBox txtBox = new TextBox();
                    txtBox.Text = "TextBox - " + i.ToString() ;                  
                    //phTextBoxes.Controls.Add(txtBox);
                    //tdTextBoxes.Controls.Add(txtBox);

                    pnlTextBoxes.Controls.Add(txtBox);
                     pnlTextBoxes.Controls.Add(new LiteralControl("<br />"));  
                    //pnlTextBoxes                
                }
            }
            // Generate Button Controls
            else
            {
                for (int i = 1; i <= Count; i++)
                {
                    Button btn = new Button();
                    btn.Text = "Button - " + i.ToString();
                    //phButtons.Controls.Add(btn);
                    //tdButtons.Controls.Add(btn);

                    pnlButtons.Controls.Add(btn);
                    pnlButtons.Controls.Add(new LiteralControl("<br />"));
                }
            }
        }
    }
  }
}
Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
  • Please don't post just a bunch of code without an explanation as to how it helps answer the users question. (You can edit your post to add an explanation.) – Loren Oct 04 '17 at 18:35