Asp. NET Label control usage-Basic application

Source: Internet
Author: User

The Label control provides a way to programmatically set text in an ASP.net web page. You can usually use a Label control when you want to change text in a Web page at run time, such as a response button click.

One, property

The common properties and descriptions for the label control are shown in table 1.

Table 1 Common properties and descriptions for label controls

Property Description
Id The ID name of the control
Text The text that the control displays
Width Width of control
Visible Control is visible
CssClass The style that the control renders
BackColor control's background color
Enabled Control is available

Some of the key properties of the label control are described in detail below.

1.ID Properties

The id attribute is used to uniquely identify the label control, and the program developer can invoke the property, method, and event of the control using the ID attribute during programming. You can set the ID property through the Properties dialog box, as shown in Figure 1.

Figure 1 Label control property settings

Attention:
(1) The property settings of the label control can also be implemented through the HTML code, which implements the following code:

Copy Code code as follows:

<asp:label id= "Label1" runat= "Server" text= "Label Example"
Backcolor= "#FF8000" bordercolor= "Blue"
cssclass= "Stylecs.css" font-names= "Song Body" font-size= "9pt"
Forecolor= "Black" height= "13px" width= "57px" >
</asp:Label>

(2) The properties of all of the following controls can be set through the Properties dialog box and will not be discussed later.

2.Text Properties

The Text property is used to set the textual content displayed by the label control, as shown in Figure 1.

3.CssClass Properties

Before you set the CssClass property of a Label control, first write the following code in the

Copy Code code as follows:

<link href= "Stylecs.css" rel= "stylesheet" type= "Text/css"/>

Then set the control's CssClass property in the Properties dialog box to Stylecs (Stylecs is the style name).

Second, the method

The common methods and descriptions for label controls are shown in table 2.

Table 2 Common methods and descriptions for label controls

Method Description
ApplyStyle Copies all the Non-white-space elements of the specified style to a Web control, overwriting all existing style elements of the control
Applystylesheetskin Apply the style attributes defined in the page style sheet to the control
Copybaseattributes Copies a property that is not encapsulated by a style object from the specified Web server control to the Web server control from which this method is called
DataBind To bind a data source to a called server control and all its child controls
Focus Set input focus for a control
Dispose Enables a server control to perform a final cleanup operation before releasing from memory
Equals Determines whether two instances of object are equal
FindControl Searches for the specified control ID in the current naming container
GetHashCode As a specific type of hash function
GetType Gets the type of the current instance
Hascontrols Determine if the server control contains any child controls
Mergestyle Copies all the Non-white-space elements of the specified style to the Web control without overwriting any existing style elements of the control
ReferenceEquals Determines whether the specified object instance is an equal instance
RenderBeginTag Renders the HTML opening tag of the control to the specified writer
RenderControl Output server control content and store trace information about this control (if tracing is enabled)
RenderEndTag Renders the HTML end tag of the control to the specified writer
Resolveclienturl Converts a URL to a URL that is available at the requesting client
ResolveUrl Converts a URL to a URL that is available at the requesting client
Setrendermothoddelegate Assign an event handler delegate to render the server control and its contents into the parent control
Tostring Returns a String that represents the current object

Description

The DataBind method is used primarily to perform data-binding operations, which are commonly used in data-bound controls such as the GridView control, and are not described here, as shown in the use and examples of the DataBind method of the GridView control. In addition, the focus method is often used to set the focal point for a TextBox control, which is used in the TextBox control focused method.

III. Events

The common events and descriptions for the label control are shown in table 3.

Table 3 common events and descriptions for label controls

Events Description
DataBinding Event raised when a server control is bound to a data source
Load Event raised when a server control is loaded into a Page object

If you implement functionality under an event in a Label control, you can click the image004 icon button in the Properties dialog box, locate the event, and then double-click to write code in its background page. For example, if a user wants to load a label control directly into the Page object while the page is executing, you can write the following code directly under the Load event of the label control:

Copy Code code as follows:

protected void Label1_load (object sender, EventArgs e)
{
Label1.Text = "Label Control event code writing";
}

Iv. examples

Label Control Sample

The following example controls the display appearance of a label control primarily by setting its associated properties. Create a new Web site, the default home page is default.aspx, add a label control on the Default.aspx page, and its property settings are shown in table 4.

Table 4 Label control property settings

Property name Property Value
Id Labtest
Text label Example
BackColor Green (Greens)
BorderColor Blue (Blues)
BorderWidth 2PX (2 pixels)
Font-name Song Body
Font-size 24pt (24 lb)
ForeColor Whites (white)

Executes the program, and the sample runs as shown in Figure 2.

Figure 2 Example of a Label control

The complete code for the program is as follows:

Default.aspx.cs Code files

Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}

protected void Labtest_load (object sender, EventArgs e)
{
Labtest.focus ();
}
}

Default.aspx Design Documents

Copy Code code as follows:

<%@ 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 ">
<title>label Control Sample </title>

<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:label id= "labtest" runat= "Server" backcolor= "Green" bordercolor= "Blue" font-bold= "True"
Font-names= "Song Body" font-size= "24pt" forecolor= "white" height= "20px" onload= "Labtest_load"
text= "Label Example" width= "167px" borderwidth= "2px" ></asp:Label></div>
</form>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.