Infolink

 

Search This Blog

Nov 16, 2012

Asp.Net Tutorial (PART-15)

Displaying Information

The ASP.NET Framework includes two controls you can use to display text in a page: the Label control and the Literal control. Whereas the Literal control simply displays text, the Label control supports several additional formatting properties.


Using the Label Control

Whenever you need to modify the text displayed in a page dynamically, you can use the Label control. For example, the page in Listing 2.1 dynamically modifies the value of a Label control’s Text property to display the current time



<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
void Page_Load()
{
lblTime.Text = DateTime.Now.ToString(“T”);
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>
Show Label</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:Label id=”lblTime” Runat=”server” />
</div>
</form>
</body>
</html>


Any string that you assign to the Label control’s Text property is displayed by the Label when the control is rendered. You can assign simple text to the Text property or you can assign HTML content.

As an alternative to assigning text to the Text property, you can place the text between the Label control’s opening and closing tags. Any text that you place before the opening and closing tags gets assigned to the Text property.

By default, a Label control renders its contents in an HTML &lt;span&gt; tag. Whatever value you assign to the Text property is rendered to the browser enclosed in a &lt;span&gt; tag.

The Label control supports several properties you can use to format the text displayed by the Label (this is not a complete list):
  • BackColor—Enables you to change the background color of the label.
  • BorderColor—Enables you to set the color of a border rendered around the label.
  • BorderStyle—Enables you to display a border around the label. Possible values areNotSet, None, Dotted, Dashed, Solid, Double, Groove, Ridge, Inset, and Outset.
  • BorderWidth—Enables you to set the size of a border rendered around the label.
  • CssClass—Enables you to associate a Cascading Style Sheet class with the label.
  • Font—Enables you to set the label’s font properties.
  • ForeColor—Enables you to set the color of the content rendered by the label.
  • Style—Enables you to assign style attributes to the label.
  • ToolTip—Enables you to set a label’s title attribute. (In Microsoft Internet Explorer, the title attribute is displayed as a floating tooltip.)
In general, I recommend that you avoid using the formatting properties and take advantage of Cascading Style Sheets to format the rendered output of the Label control. The page in Listing contains two

Label controls: The first is formatted with properties and the second is formatted with a Cascading Style Sheet

<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<style type=”text/css”>
div
{
padding:10px;
}
.labelStyle
{
color:red;
background-color:yellow;
border:Solid 2px Red;
}
</style>
<title>
Format Label</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:Label id=”lblFirst” Text=”First Label” ForeColor=”Red” BackColor=”Yellow” BorderStyle=”Solid”
BorderWidth=”2” BorderColor=”red” Runat=”server” />
<br />
<asp:Label id=”lblSecond” Text=”Second Label” CssClass=”labelStyle” Runat=”server” />
</div>
</form>
</body>
</html>

You should use a Label control when labeling the fields in an HTML form. The Label control includes a property named the AssociatedControlID property. You can set this property to point at an ASP.NET control that represents a form field.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...