What is LiteralControl? Why is it used?

2020-02-09 07:19发布

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条回答
闹够了就滚
2楼-- · 2020-02-09 07:55

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.

查看更多
虎瘦雄心在
3楼-- · 2020-02-09 08:07

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 />") ); 
    }
}
查看更多
Root(大扎)
4楼-- · 2020-02-09 08:09

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

查看更多
乱世女痞
5楼-- · 2020-02-09 08:10

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.

查看更多
叼着烟拽天下
6楼-- · 2020-02-09 08:10
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 />"));
                }
            }
        }
    }
  }
}
查看更多
做个烂人
7楼-- · 2020-02-09 08:12

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.

查看更多
登录 后发表回答