In this Part we are working with some of the Standard Controls in Details.
Standard Controls
Standard controls contain controls which help the developers to perform useful operations for example displaying information, taking user input in a textbox, displaying a drop down box, ListBox and many more. we will take look at some of them in detail.
Label Control
label control is used when you need to dynamically display/change data on the webpage. we already saw a sample code in the last part (part 03) which changed text of the label dynamically on button’s click and TextBox’s TextChanged Event. There are plenty of Properties with which we play around with.
following are some of the most used properties witha brief description
- BackColor – sets the color of the background of the label control
- BorderColor – sets the color of the border of the label control
- BorderStyle – lets you set the style of the border e.g. NotSet, None, Dotted, Dashed, Solid, Double, Groove, Ridge, Inset, and Outset.
- BorderWidth – lets you set the width of the border
- CssClass -lets you set CSS class for label.
- Font – lets you configure various font properties e.g Bold, Italic, Name, Names, Overline, Size, Strikeout, Underline
- ForeColor – lets you set the color of the content of the label.
- ToolTip – lets you set the tool tip for the label control.
- Style – lets you set inline style attribute for the label control.
now lets have a look at a sample which illustrates the use of these properties.
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title>[.toString()] Standard Controls - http://dottostring.com</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1"
runat="server"
Text="label text goes here"
BackColor="#FF9966"
BorderColor="Black"
BorderStyle="Double"
BorderWidth="2px"
Font-Bold="True"
Font-Italic="True">
</asp:Label>
</div>
</form>
</body>
</html>
- Run (F5) your website and this is what you will get:
Labels are frequently used as the labels next to textbox fields in order to identify the information to be entered in those fields. In order to associate a Label to a TextBox, Label has a property AssociatedControlID, setting this property doesn’t change anything as far as the display is concerned however when the label is clicked by the user it automatically takes the focus to the associated control.
Here is another sample which illustrates the use of AssociatedControlID and CssClass attributes.
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title>[.toString()] Standard Controls - http://www.dottostring.com</title>
<style type="text/css">
.LabelStyle {
font-family: "Trebuchet MS";
font-size: medium;
font-weight: bold;
color: #800000;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div >
<asp:Label ID="Label1"
runat="server"
Text="label text goes here"
BackColor="#FF9966"
BorderColor="Black"
BorderStyle="Double"
BorderWidth="2px"
Font-Bold="True"
Font-Italic="True">
</asp:Label>
<br />
<br />
<asp:Label
ID="lblProductName"
runat="server"
AssociatedControlID="txtProductName"
Text="Product Name"
CssClass="LabelStyle">
</asp:Label>
<br />
<asp:TextBox
ID="txtProductName"
runat="server">
</asp:TextBox>
<br />
<asp:Label
ID="lblProductCode"
runat="server"
AssociatedControlID="txtProductCode"
Text="Product Code"
CssClass="LabelStyle">
</asp:Label>
<br />
<asp:TextBox
ID="txtProductCode"
runat="server">
</asp:TextBox>
</div>
</form>
</body>
</html>
- Run(F5) the website and see the results.
Literal Control
Literal Control is almost the same as the Label Control except for the fact that Literal Control does not render its content inside the Span Tag, you can use it to display text or HTML content. it simply outputs contents assigned to it. Like for example lets say we need to dynamically update the title of the page, this can be done either by placing a runat=”server” and ID attributes to the Title Tag and then modify it from the server side or we can simply place a Literal Control in the Title Tag and then assign the desired title text to the literal control from the server.
Literal Control possess a useful property called Mode, which can be set to one of the following properties.
- PassThrough – Everything assigned to the Text Property is rendered as it is to the browser. for example “<b><i>dottostring.com</i></b>” will print dottostring (in bold and italic)
- Transform – This will remove all the content from the Text Property of the Literal Control which is not supported by the browser/device it is rendering to. In case of HTML Browser this acts just like PassThrough
- Encode – This will encode the all the tag so that they are displayed as it is to the viewer. for example “<b><i>dottostring.com</i></b>” will print <b><i>dottostring.com</i></b> to the user interface.
as we dont have any span tag in the literal control rendered output, therefore we cannot apply any sort of formatting.
now lets look at some samples
- Add Two Literal Controls, one in the Title Tag and the other inside the body and name them lTitle and lContent respectively.
- in the Page_Load Method add the following code.
protected void Page_Load(object sender, EventArgs e)
{
lTitle.Text = "[.toString()] Standard Controls - http://www.dottostring.com";
lContent.Text = "<b><i>dottostring</i></b>";
}
- Run(F5) the application and notice the following results

ASP.NET Standard Controls - Literal Control Demonstration
The Title of the page is what we set from the Page_Load Method dynamically and also we can see the other Literal Control which is rendered to display dottostring in below the two textboxes. also it is important to note that by Default the Mode Property for Literal Control is set to Transform, and since results are being redered to an HTML Browser, Transform and PassThrough will generate the same results, however when we change the IContent’s Mode Property to Encode it will also display the markup language on the user interface. effectively converting “<” to “<” and “>” to “>”.

ASP.NET Standard Controls - Literal Control Demonstration - Mode=Encode
we can clearly see the mark up language now. so Literal Control can be a very handy control in generating HTML content dynamically.
Thats it for this Part04, in this part we learned how to use some Standard Controls and what their important set of properties do. In the next part we will look at other Standard Controls which will enable us to make our interface much more user interactive.
- Thanks for visiting and waiting patiently for this part, I will be posting more regulary now, so keep in touch and don’t forget to leave comments/feedback
- Download Source Code for Part04
- Cheers !
Previous Parts:


















