What is LiteralControl? Why is it used?

Viewed 33427

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?

7 Answers
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 />"));
                }
            }
        }
    }
  }
}
Related