Asp.Net: Literal Vs Label

ASP.Net: Literal vs Label

Yep, the main difference is that Literal controls just render out text, but Label controls surround it with <span> tags (Unless you use the AssociatedControlID property, in which case a Label control will render a <label> tag).

So, labels can be styled easier, but if you're just inserting text, literals are the way to go. Literal controls also have a handy property Mode which governs how the text is rendered. You can have it HTML-encoded, or rendered without any changes, or have any "unsupported markup-language elements" removed.

If you're not applying any styles (e.g. by using Label's CssClass property), it will be fine to replace Label controls with Literal controls.

What's the Literal control used for and what's the difference to the Label Control in asp.net?

The major difference is that the Label Control adds the span tag to the text (property) you set, allowing to apply a style to it:

<span>My Label text</span>

The Literal Control allows you to render any kind of content. You can use it to render scripts, hmtl and any other type of document content. It doesn't change the string you provide in the Text property.

Note: the Label control allows you to render straight HTML too, but it puts all your text in span tags as mentioned. So, for rendering large HTML portions a Literal control is the way to go.

P.S.: In HTML there is a <label> tag. If you use the AssociatedControlId property of the Label control, it will render as HTML <label> (thanks to Ray for pointing that out.)

For example:

<asp:Label runat="server" id="FirstNameLabel" AssociatedControlId="FirstNameTextBox">
Input First Name:
</asp:Label>
<asp:Textbox runat="server" id="FirstNameTextBox" />

Will render as:

<label for="FirstNameTextbox" id="FirstNameLabel">Input first name:</label>
<input type="text" id="FirstNameTextbox" name="FirstNameTextBox" />

See also here on W3 Schools.

What is LiteralControl? Why is it used?

From MSDN Literal Control represents HTML elements, text, and any other strings in an ASP.NET page that do not require processing on the server.

Also have a look at this for Literal Control usage

A label renders a <span> tag. It allows you to programmatically change the
display characteristics of some text you want to display. A LiteralControl
renders exactly whatever static HTML you want it to. There is no general
advantage of using one over the other. One may be more useful than another
in certain circumstances. For example, if you want to display static text,
and make no programmatic changes to it, you might want to use a
LiteralControl.

PlaceHolder vs Literal for adding HTML markup generated at runtime

Neither render any markup of their own (which can be a very good thing). However, a Placeholder may contain child controls, whereas a Literal cannot.

By comparison, a Placeholder can contain other controls, but does not have a Text property.

I'm wondering which control/approach should we use for just adding
generated markup and nothing more.

If by "generated" you mean the end result is a string, I would use a Literal. If you are generating a control tree, then append those controls to a Placeholder.

Or, if you want to omit the declaration of a server control completely:

<h2>Hello World</h2>
<p>The following is generated markup.</p>
<%= base.GetGeneratedMarkup() %>

I believe a Literal is still generated under the hood for this, but it allows you to mix generated content with the markup portion of your page/control (similar to Razor).

How to update ASP Web application Literal or Label after user presses button

You can use Java Script Function like

 <script type="text/javascript" language="javascript">
function javascriptFunction()
{

}
</script>

call this function
If you Used Update Panels Then You can Use in .Cs Page:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

or On Button's Click in .aspx page

<asp:Button ID="Save" runat="server" Text="Save Changes" OnClick="SaveChanges" 
onClientClick="javascriptFunction();"
ValidationGroup="ProjectSummaryValidationGroup"
meta:resourcekey="SaveResource1" />

Other Wise You can Use in .cs page

ClientScript.RegisterStartupScript
(GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);

set value of Label in javascript:

document.getElementById('<%=SaveTime.ClientID%>').value = "Your Date";

for label :

void Page_Load(object sender, EventArgs e) 
{
lblMyLabel.Attributes.Add("onclick",
"javascript:alert('ALERT ALERT!!!')");
}

Mode property of Literal control in asp.net

When you have a question about something built into .NET, the place to check is MSDN. Here's the relevant documentation for the Mode property.

Here's what is says about transform.

Unsupported markup-language elements are removed from the contents of
the control. If the Literal control is rendered on a browser that
supports HTML or XHTML, the control's contents are not modified.

Also

If you specify Transform, the rendering behavior of the Text property
depends on the type of markup being rendered. When the Literal control
is rendered for a device or browser that supports HTML or XHTML,
specifying Transform produces the same behavior as specifying
PassThrough. All markup tags and elements for the Text property are
rendered for the requesting browser.

Difference between Localize vs Literal

As you quoted, they are identical in every way.

The only difference is that one is a Localize. That's it. That would enable other classes to treat the Literal differently, if they wanted to. But again: they are identical.

It's essentially just a marker class.

Label/Literal control with formatted text field

To solve my problem I have actually had a second look at how I am displaying content and found that a lot of times the Literals and Labels could be dropped in place of plain HTML code. I can then use my preferred method <%= ... %> to display content.

Literal Control in ASP.NET HTML Table

it appears that you are mixing Html Tables and ASP.NET.

If you change your Html Table to:

<asp:Table runat="server" ID="tblSubstantialOwners">
<asp:TableHeaderRow ID="tr_header" runat="server">
<asp:TableHeaderCell>
<asp:Label ID="lblOnwerName" Text="Name" runat="server"></asp:Label>
</asp:TableHeaderCell>
<asp:TableHeaderCell>
<asp:Label ID="lblOwnerAddress" Text="Address" runat="server"></asp:Label>
</asp:TableHeaderCell>
<asp:TableHeaderCell>
<asp:Label ID="lblOwnerTIN" Text="TIN" runat="server"></asp:Label>
</asp:TableHeaderCell>
</asp:TableHeaderRow>

<asp:TableRow>
<asp:TableCell>
<asp:TextBox ID="txtOwnerName1" Width="80px" runat="server" AutoCompleteType="Disabled"
MaxLength="20" />
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtOwnerAddress1" Width="80px" runat="server" AutoCompleteType="Disabled"
MaxLength="20" />
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtOwnerTIN1" Width="80px" runat="server" AutoCompleteType="Disabled"
MaxLength="20" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>

And your code to:

protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (System.Web.UI.WebControls.TableRow row in tblSubstantialOwners.Rows)
{
if (row.GetType() == typeof(TableRow))
{
for (int count = 0; count < row.Cells.Count; count++)
{
TableCell cell = row.Cells[count];
datarow[count] = cell.Controls.OfType<TextBox>().FirstOrDefault().Text;
}
}
}
}

You can get to the controls you have in your table cells directly, either by name, ordinality, or type...

ASP.Net literal. Insert html

A div inside a P is invalid HTML.
I don't think .net is making the

up.
I think it comes from your browser.



Related Topics



Leave a reply



Submit