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?
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.
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.
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.
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.
A LiteralControl is used to inject HTML into your page at runtime.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With