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