<literal> vs <label> displays

A

AAaron123

I'm confused about what my code displays.

I want to be sure I understand the following.

Does it mean that if I put markup into the literal's text it will display as
raw text

Does it mean that a label control will format markup before it is displayed?

That's the way I read it.



<literal>

Use the System.Web.UI.WebControls..::.Literal control to reserve a location
on the Web page to display text. The Literal control is similar to the Label
control, except the Literal control does not allow you to apply a style to
the displayed text. You can programmatically control the text displayed in
the control by setting the Text property.



<label>

Use the Label control to display text in a set location on the page. Unlike
static text, you can customize the displayed text through the Text property.

You can also use the Literal and PlaceHolder controls to display text on the
Web Forms page. However, unlike the Label control, these controls do not
render any additional tags.



thanks
 
N

Nathan Sokalski

Take a look at the Mode attribute of the Literal control. This attribute is
used to specify whether the value specified for the Text attribute is
rendered exactly as it is entered or formatted as HTML. Also, the Literal
and PlaceHolder have somewhat different purposes. The PlaceHolder is used
when you need to be able to add child controls at a specific location, but
do not have any controls that you know will want there. The Literal, on the
other hand, is simply used to include a simple string of text rather than
html generated by a control (yeah, I know, Literal is technically a control,
and it does generate html, but in most cases that "html" is exactly the same
as the text attribute). The reason for having both the Literal and Label
controls is because the Label allows you to add style and do several other
things, the reason for having the Literal is simply to allow you an easy way
to programmatically insert text into the generated output of the page.
Hopefully this helps.
 
G

Gregory A. Beamer

I'm confused about what my code displays.

I want to be sure I understand the following.

Does it mean that if I put markup into the literal's text it will
display as raw text

Does it mean that a label control will format markup before it is
displayed?


Yes, but let's make sure this is clear.

If you have the following:

lit1.Text = "<script>alert('blah!');</script>";
label1.Text = "<script>alert('blah!');</script>";

The literal will have the JavaScript popup and the label will show the
actual text.

If that is what you think it means, you are right.
 
A

AAaron123

I think I have the Literal in hand but not so for the PlaceHolder.

I tried:
<asp:Literal ID="Literal1" Mode="Encode" Text="<input type='button'
value='new button' onclick='alert("clicked")'/>"

runat="server">

</asp:Literal>

And it added a button that worked.

So when do I need PlaceHolder?

To add runat server controls?



Thanks
 
A

AAaron123

Thanks for the three replies.

I think I have it now!

If I Encode <hr /> the browser displays <hr />

But if I don't encode it changes it to a line.

What I find a little upsetting si that that seems to make sense to me :)


Thanks
 
N

Nathan Sokalski

PlaceHolder is used when you want to add controls programmatically. This
means that if you have a PlaceHolder that is declared as follows:

<asp:placeHolder ID="phExample" runat="server"/>

You will add child controls to it like this:

Dim mychildctrl As New Label()
Me.phExample.Controls.Add(mychildctrl)

Basically, the PlaceHolder is a control that has nothing other than child
controls. If you don't add any child controls (and it is possible that in
some scenarios your code may dynamically determine that you don't want to),
than the control will render absolutely nothing. Hopefully this helps.
 
A

AAaron123

Thanks

Nathan said:
PlaceHolder is used when you want to add controls programmatically.
This means that if you have a PlaceHolder that is declared as follows:

<asp:placeHolder ID="phExample" runat="server"/>

You will add child controls to it like this:

Dim mychildctrl As New Label()
Me.phExample.Controls.Add(mychildctrl)

Basically, the PlaceHolder is a control that has nothing other than
child controls. If you don't add any child controls (and it is
possible that in some scenarios your code may dynamically determine
that you don't want to), than the control will render absolutely
nothing. Hopefully this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top