Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is LiteralControl? Why is it used?

Tags:

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?

like image 851
pavanred Avatar asked Jul 18 '10 04:07

pavanred


People also ask

How do you apply Literal control style?

Label controls do the same but wrap them in a span which you could do apply a style to. Change your Literal to a Label and you should be good to go. Show activity on this post. In simple asp:Literal is a style less control, so enclose this with another element like div or span and give style to this element.

What is ASP content tag?

asp:Content ID="HeaderContent" is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated. head is a standard html markup, indicating the page head elements.

What is the difference between label and Literal control in asp net?

Label control can be styled i.e. its Font, Color, Font Size, etc. can be easily changed but Literal control cannot be styled as it does not use any HTML tag. Label control enables to display static text on the web page. while Literal control is used most frequently when adding content dynamically to the page.

What is an ASP panel?

ASP.NET Core API. The Panel control works as a container for other controls on the page. It controls the appearance and visibility of the controls it contains. It also allows generating controls programmatically.


2 Answers

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

like image 170
Sky Sanders Avatar answered Oct 01 '22 04:10

Sky Sanders


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.

like image 21
ACP Avatar answered Oct 01 '22 04:10

ACP