Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Advertisements


Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
asp:Label control
Label control is used to place a static, non clickable piece of text.
 
Ideally Label control is used to place a static, non clickable (can't fire onclick event) piece of text on the page. When it is rendered on the page, it is implemented through <span></span> HTML tag. Its properties like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. are implemented through style properites of <span>. You can set its Text property either by setting Text properties in the .aspx page or from server side page. (other properties can also be set from both pages)

Following are few properties of the Label that are very useful.
EnableViewState true/false. If false ViewState will not be maintained.
Visible true/false. If false control will not be rendered to the page
DEMO : Label Show Source Code
Ex. Example of Label Control
                        
                        // Label control code
                        <asp:Label ID="Label2" runat="server" BackColor="Coral" 
                        ForeColor="blue" BorderColor="ActiveBorder" 
                        BorderStyle="dashed" BorderWidth="1" Height="20" 
                        Text="Example of Label Control" Width="200"
                        ></asp:Label>
                        
                        // Rendered HTML code of the Label
                        <span id="ctl00_PlaceHolderForContents_Label1" 
                        style="display:inline-block;color:Blue;
                        background-color:Coral;
                        border-color:activeborder;border-width:1px;
                        border-style:Dashed;
                        height:20px;width:200px;">
                        Example of Label Control</span>
                    
                        // Fires when Button is clicked
                        protected void ChangeLabelText(object sender, EventArgs e)
                        {
                            Label1.Text = TextBox1.Text;
                        }
                         
                    
There is one very important but rarely used properties of Label control is AssociatedControlID that is used to associate a Label with a particular Control. Like in this example, Click anywhere in the page except "Write something into the TextBox" in the demo box then click "Write something into the TextBox" and notice that Cursor is placed in the TextBox.