Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Literal vs LiteralControl?

Tags:

c#

asp.net

What are the appropriate use of these two controls? From time to time I build up HTML in the code behind. Sometimes I want to output white space and I end up doing something like this.

const string twoSpaces = "  "; p.Controls.Add(new Literal { Text = twoSpaces }); 

or

const string twoSpaces = "  "; p.Controls.Add(new LiteralControl { Text = twoSpaces }); 

My question is, is this an appropriate use of these controls? Should I be adding whitespace this way? When do I use one over the other?

I realize I could probably do something with CSS, but I really want to know what are the purposes of these two controls, and is there anything inherently wrong with using them in this fashion.

like image 299
Hcabnettek Avatar asked Dec 04 '09 16:12

Hcabnettek


People also ask

What is difference between label control and Literal control?

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 the use of Literal?

Literals provide a means of expressing specific values in your program. For example, in the following statement, an integer variable named count is declared and assigned an integer value. The literal 0 represents, naturally enough, the value zero. Code section 3.61: Numeric literal.

What is the use of Literal control in asp net?

The Literal control is used to display text; that is, it renders static text on a Web page without adding additional HTML tags. It passes content directly to the client browser unless you use the Mode property to encode the content.

Which is a valid property for Literal control?

The Literal control has the following properties: EnableViewState: A Boolean property signaling whether the control persists its view state (and the view state of any child controls it contains) to the requesting client. The default value is true. ID: A string value identifying the control on the page.


2 Answers

Literal uses ViewState and will remember any updates to its properties across Postbacks. LiteralControl must have its properties set on every postback.

Other than that, be very careful when using Literal. If you allow user input to be rendered in a Literal tag, you have very likely opened up a XSS attack.

like image 58
Chris Pitman Avatar answered Sep 18 '22 15:09

Chris Pitman


Chris Pitman has answered it correctly, but I just want to add some more facts about both.

Literal is a control which can be added on both an aspx page as well as to the code behind.

asp:Literal Text="text" runat="server"

But LiteralControl cannot be added on aspx page, but only to the code behind. So that is why LiteralControl doesn't have its ViewState associated to the page class.

like image 41
Ankur Bhutani Avatar answered Sep 19 '22 15:09

Ankur Bhutani