Results 1 to 5 of 5

Thread: [2005] Whats the difference,..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    [2005] Whats the difference,..

    between using the <asp:Literal,....> and
    <b> My Name is Rauland,.....

    Code:
     <div>
        <asp:Literal runat="server" Text="My Name is Rauland,uses literal" /><br />
        <b> My Name is Rauland, no literal.</b>
        </div>
    So just to display plain text, which should I use?

    According to the help:

    Use the System.Web.UI.WebControls.Literal control to reserve a location on the Web page to display text. (I´m aware that by using Mode="Encode", you can show HTML text as is).

    On the other hand LiteralControls are, acorrding to the help: (Literal and LiteralControl are different )

    ASP.NET compiles all HTML elements and readable text that do not require server-side processing into instances of this class. For example, an HTML element that does not contain a runat="server" attribute/value pair in its opening tag is compiled into a LiteralControl object.

    So I suppose this means that the:
    <b> My Name is Rauland, no literal.</b>, would be compiled into an instance of a LiteralControl.

    So the question is:
    Literal control to display plain text?
    Or just write the text?

    I´m a little confused.
    (Both of them are compiled in to controls on the server, either Literal, or LiteralControl)

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] Whats the difference,..

    The difference is that you can change what the Literal shows. In a few websites that I'm maintaining, literals are used to fire JavaScript commands without disrupting the flow of the page. User clicks on a button, the literal's Text field is populated with the correct JS, and it immediately executes.

  3. #3
    Hyperactive Member kayos's Avatar
    Join Date
    Apr 2004
    Location
    Largo, Florida
    Posts
    306

    Re: [2005] Whats the difference,..

    Also, a Literal is rendered AS IS. For instance if you put your text into a Label control, then when it is rendered to the browser it is in a <span> tag. All the other controls also have accompanying HTML tags, where as the Literal control does not.


    If this post helps, please RATE MY POST!

    Using Visual Studio 2005 SE

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    Re: [2005] Whats the difference,..

    Thanks,
    TimeShifter, you mentioned that after a postback to the server, the literal is populated with Javacript? without disrupting the flow of the page, and the the JS code fires?.

    Could you show the simplest of examples?
    Thanks.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Whats the difference,..

    The literal control takes any HTML you give it. So you could say

    Literal1.Text = "<h1>hello!</h1>";

    You could therefore give it javascript as well:

    Literal1.Text = "<script type=\"text/javascript\">alert('Hello');</script>";

    But that isn't a recommended way to send javascript to the browser. Instead, use

    Page.RegisterStartupScript("myscript","<script type=\"text/javascript\">alert('Hello');</script>");

    or use ClientScript.RegisterStartupScript

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width