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

What Literal control is used for in asp.net? and What is the difference between them and Label control?

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.

One thing also to note is if you are justing using it to display something and have no need for formatting the text use a Literal control. The ViewState is not as heavy with a Literal vs a Label control and when you have many of these on a page using ViewState it can really bloat your page size.
I always ask myself, do I need to apply a custom style or formatting? Yes, use a Label. No, use a Literal.

It is used to display text on the page, the text that is displayed can be set at runtime via server side code.

The label control also has the AssociatedControlId property that associates the label with another control. An example of where this is useful is with a textbox control. Once these are associated, screen readers are more able to give better results.
Another example is a radio button with a label allows you to click on the label and the radiobutton will select if the AssociatedControlId property is set.
MSDN on AssoicatedControlId

As splattne mentions, the label encloses its text in a span, whereas the literal is simply a placeholder. However, be careful in making assumptions about how ASP.Net controls are going to render. It can depend on the user agent that you are using. For instance, the panel control renders as a div in IE, but renders as a table with Firefox.

It will place LITERALLY whatever text you place in it on the page. You can use it to write html, JavaScript or just plain text.

We can use literal control in the title tag whereas label cannot be used in the title tag
Label can be used to set focus on other controls like Textbox.
Whereas Literal simply rander the static text on the web page

Related

How to Display as Html content in Textbox

How to Display Html content in Textbox Control
Eg:
string text="<b>" + Hi How are you + "</b>";
txtEditor.Text=text.ToString();
I have to display those text in bold Letters, How do i achieve this
You can change the font property of the TextBox:
txtEditor.Font = new Font(txtEditor.Font, FontStyle.Bold);
See this link for an existing html editor. I strongly recommend that you carefully read the paragraph about Cross Site Scripting, whether you decide to use that control or not.
To display text bold in asp:textbox control, you can use style at design time or runtime
in .aspx page Design time
<asp:TextBox ID="textBox1" style="font-weight:bold;" runat="server"></asp:TextBox>
OR
in Code Behind
textBox1.Font.Bold = true;
You can try any one to get desired result. My suggestion over here is that use CSS style to get more advanced formatting in textbox

Asp.Net - User control with text between blocks

I want to create a usercontrol that behaves like the Label usercontrol or the HyperLink usercontrol.
What I mean - the Label usercontrol has the Text attribute, and the text can also be set with the following way:
<asp:Label runat="server" id="lblTest">Text Here</asp:Label>
If I wish to create a usercontrol that can set the text between blocks to the actual Text attribute of the control.
Do I need to use Templated UserControl? If no - what is the correct way?
Thanks all.
I would create a simple custom control with the text attribute exposed as a property :-)
http://msdn.microsoft.com/en-us/library/aa310915%28VS.71%29.aspx

Inconsistencies When Accessing DataBound Item Controls

Essentially, my question is this: There are two ways that I have experienced setting values to databound controls. Either this way:
<asp:Label runat="server" id="MyLabel"><%#DataBinder.Eval(Container.DataItem, "MyValue")%></asp:Label>
Or this way:
<asp:Label runat="server" id="MyLabel" text=<%#DataBinder.Eval(Container.DataItem, "MyValue")%> />
When trying to access items in an event handler (outside of the method that this databinding is occuring) using the first method, MyLabel.Text is an empty string. However, using the second way, MyLabel.Text will equal "MyValue". Can anyone tell me why this happens?
The Text property of a Label doesn't map to the inner text in the control markup. The Label control can be used as a container for other controls - so you'd put child controls inside the tag.
The reason you're seeing the Text as empty when you bind using <%# ... %> is because the bound text is rendering as a child literal control in the MyLabel.Controls collection. In this case, you'd access the text as
var myText = ((ITextControl)MyLabel.Controls[0]).Text;
// instead of..
var myText = MyLabel.Text;
If you want to access the Text of the label - always use the Text property. If you want to nest controls in your label - put them between the markup tags.
Not sure about this but... It may be because in the second example, Text is a property of the Label control and you set it directly, whereas in the first example, you're not setting the Text property, you're just adding a child to the Label...
EDIT: A quick look with Reflector confirmed this: if the Label has some child content to it, it is that content that is rendered to html (but it's never set to the Text property). Otherwise, it is the content of the Text property that is rendered.

asp.net: difference between runat="server" and server controls

What is the difference in functionality between
<asp:Button id="button1" Text="Click me" runat="server" OnClick="submitEvent" />
and
<input type="button" id="button1" runat="server" value="Click me" />
Does the input with runat="server" attribute has other or limited properties and methods?
Thank you!
The first one creates a System.Web.UI.WebControls.Button while the second one creates a System.Web.UI.HtmlControls.HtmlInputButton.
Both are server controls, but the controls in the WebControls namespace generally has a bit more functionality than the controls in the HtmlControls namespace. Typically they put some data in ViewState to keep track of their state, and they have server side postback events.
Each controls in the HtmlControls namespace correspond exactly to an HTML element, while the controls in the WebControls namespace may be rendered differently depending on what the browser that is requesting the page can support.
The button represented by <asp:Button runat="server".../> will be converted to a web server control with a rich state model and different properties and methods which has more clear representation in real world like Button.Text = "Click Me".
The button represented by <input type="button" runat="server"..../> will be converted to html server control represented by HtmlInputButton; with has limited properties, methods and events. Most of the properties resemble the html equivalents like Button.Value="Click Me".
Note that elements in a markup page are pre-processed/compiled before being used and will be converted to a class representation where every element is represented by a control. You can access server side controls which are identified by the runat="server" tag from the code behind since they will have the correct matching server control(web/html), other static content including an <input type="button.../> tag with out the runat="server" will be represented as a LiteralControl.
The former line is ASP.NET, the latter simple XHTML.
The former gets parsed and interpreted on the server side, after which the HTML code is generated, which pretty much corresponds to your second example. The ASP.NET Button is really little more than light wrapper over th HTML input button functionality, and should be used wherever you need to handle the Click event on the server side (or in the general case any events), and is usually the way to go, since you're letting ASP.NET abstract the idea of a button on your page for you.
functionality of both the controls is same with the difference that first one is .net control and second one is html control that can be made servercontrol by using
runat="server".
and first one is rich in evants and metods thn the second one....
There is no server events associated with such a controls, but you can use it in codebehind to change it's properties.
Your second option won't probably even work. runat="server" will be rendered directly to the HTML output where it will have no functionality and will only break HTML validation.
input is an HTML element which has only HTML properties, and definitely no methods of any kind.

Multi language: asp:label aganist html:label with asp:Literal

I'm adding multi language support to a prototype web site. The site was developed using html lables which I could multilanguage using asp:literal or I could change them all to asp:labels as shown below.
<asp:label ID="lblAddress1" runat="server" Text='<%$ Resources:lblAddress1 %>' /></br>
<label><asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:lblAddress1 %>"></asp:Literal></label>
Web stuff isn't my area of expertise and the guys here don't think there is any advantage one way or the other. What would you choose and why?
<asp:Literal>
Use this control as a placeholder for any text you wish to insert in the page. The output will not be wrapped in any html markup tags (simplest).
<asp:Label>
Use this control in the same way as the , however, This control will wrap the text in html tags. These span tags allow the control to have additional properties (css styling etc.) which can be leveraged.
<label>
This html tag has semantic value in a page and is used to associate form elements with their description.
<label for="SaveLoginName">Remember Me:</label>
<input type="checkbox" id="SaveLoginName" />
A browser can use this info to provide additional accessibility features such as enabling clicking text to toggle checkbox value.
Each of these have appropriate usage scenarios.
Seems to be a matter of taste. Although I think the second option may add a little weight to the page because literals are usually wrapped in <span>

Resources