<literal> vs <label> displays

Discussion in 'ASP .Net' started by AAaron123, Jul 20, 2009.

  1. AAaron123

    AAaron123 Guest

    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
    AAaron123, Jul 20, 2009
    #1
    1. Advertising

  2. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "AAaron123" <> wrote in message
    news:...
    > 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
    >
    Nathan Sokalski, Jul 20, 2009
    #2
    1. Advertising

  3. "AAaron123" <> wrote in
    news::

    > 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.


    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    ******************************************
    | Think outside the box! |
    ******************************************
    Gregory A. Beamer, Jul 20, 2009
    #3
  4. AAaron123

    AAaron123 Guest

    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





    Nathan Sokalski wrote:
    > 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.
    > "AAaron123" <> wrote in message
    > news:...
    >> 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
    AAaron123, Jul 20, 2009
    #4
  5. AAaron123

    AAaron123 Guest

    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
    AAaron123, Jul 20, 2009
    #5
  6. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "AAaron123" <> wrote in message
    news:%...
    >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
    >
    >
    >
    >
    >
    > Nathan Sokalski wrote:
    >> 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.
    >> "AAaron123" <> wrote in message
    >> news:...
    >>> 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

    >
    >
    Nathan Sokalski, Jul 21, 2009
    #6
  7. AAaron123

    AAaron123 Guest

    Thanks

    Nathan Sokalski wrote:
    > 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.
    >
    > "AAaron123" <> wrote in message
    > news:%...
    >> 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
    >>
    >>
    >>
    >>
    >>
    >> Nathan Sokalski wrote:
    >>> 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. "AAaron123" <> wrote in message
    >>> news:...
    >>>> 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
    AAaron123, Jul 21, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chumley Walrus
    Replies:
    1
    Views:
    12,165
    =?Utf-8?B?QnJpYW4gQnJvd24=?=
    Dec 16, 2004
  2. Ganesh

    label displays in 2 lines

    Ganesh, Aug 5, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    325
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Aug 6, 2007
  3. Daniel Walzenbach
    Replies:
    3
    Views:
    111
    Jeffrey Tan[MSFT]
    Mar 4, 2004
  4. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    364
    Anonieko Ramos
    Sep 27, 2004
  5. Knut Krueger
    Replies:
    8
    Views:
    255
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page