ASP.NET Tutorial/ASP.net Controls/Label

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add style to asp:Label

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style rel="stylesheet" type="text/css">
body
{
  font-family: Verdana, Arial, Sans-Serif;
  font-size: small;
}
.heading1
{
  font-weight: bold;
  font-size: large;
  color: lime;
}
.heading2
{
  font-weight: bold;
  font-size: medium;
  font-style: italic;
  color: #C0BA72;
}
.blockText
{
  padding: 10px;
  background-color: #FFFFD9;
  border-style: solid;
  border-width: thin;
}
    
    
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label CssClass="heading1" ID="Label1" runat="server" Text="This Label Uses heading1"></asp:Label>
        <br />
        This is sample unformatted text.<br />
        &nbsp;<br />
        <asp:Label CssClass="heading2" ID="Label2" runat="server" Text="This Label Uses heading2"></asp:Label>
        <br />
        Here"s more unformatted text.<br />
        <br />
        &nbsp;<div class="blockText" id="DIV1" runat="server" >
            This control uses the blockText style. This control uses the blockText style. This
            control uses the blockText style. This control uses the blockText style.
        </div>
    </div>   
    </form>
</body>
</html>


Change asp:Label Text in page load event listener (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
   sub Page_Load(obj as object, e as eventargs)
      lblMessage.Text = "This is my first exercise!"
   end sub   
</script>
<html><body>
   <asp:Label id="lblMessage" runat="server"/>
</body></html>


Format a Label with CSS

<%@ 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>


Highlight with regular expression

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Highlighting" %>
<!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>Sample Search Highlighting</title>
   <style type="text/css">
      .myPanel { margin: 8pt; width: 50%; padding 10pt;  }
      body { font-family: Tahoma, Arial; font-size: 10pt;}
    </style>
</head>
<body>
   <form id="form1" runat="server">
      <asp:Panel ID="panSearch" runat="server" GroupingText="Search" CssClass="myPanel">
         Enter Search expression:
         <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox><br />
         <asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />
      </asp:Panel>
      <asp:Panel ID="panContent" runat="server" GroupingText="Content" CssClass="myPanel">
         <asp:Label ID="labContent" runat="server" />
      </asp:Panel>
   </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

using System.Text.RegularExpressions;
public partial class Highlighting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       labContent.Text = "Client-side validation is useful because it reduces round-trips to the server. This provides immediate feedback to the user as well as improves server performance. Unfortunately, client-side validation by itself is not sufficient. The user could be using a browser that does not support scripting (that is, using an ancient browser or has scripting turned off via the browser preferences). As well, client-side scripting is potentially vulnerable to script exploits. These can happen when a malicious user enters a javascript &lt;script&gt; block into a text field that can later cause harm when the entered data is echoed back to the browser.";
    }
   protected void btnSearch_Click(object sender, EventArgs e)
   {
      Regex myreg = new Regex(txtSearch.Text, RegexOptions.IgnoreCase);
      labContent.Text = myreg.Replace(labContent.Text, new MatchEvaluator(ReplaceSearchWord));
   }
   public string ReplaceSearchWord(Match m)
   {
      return "<span style="font-weight:bold; background: yellow;">" + m.Value + "</span>";
   }
}


Import properties of The Label control

BackColor:      change the background color of the label.
BorderColor:    set the color of a border rendered around the label.
BorderStyle:    display a border around the label. Possible values are NotSet, None, Dotted, Dashed, Solid, Double, Groove, Ridge, Inset, and Outset.
BorderWidth:    set the size of a border rendered around the label.
CssClass:       associate a Cascading Style Sheet class with the label.
Font:           set the label"s font properties.
ForeColor:      set the color of the content rendered by the label.
Style:          assign style attributes to the label.
ToolTip:        set a label"s title attribute. (In Microsoft Internet Explorer, the title attribute is displayed as a floating tooltip.)
      

<%@ 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>


Label controls are used to label the two TextBox controls.

<%@ 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">
    <title>Label Form</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
        id="lblFirstName"
        Text="First Name:"
        AssociatedControlID="txtFirstName"
        Runat="server" />
    <br />
    <asp:TextBox
        id="txtFirstName"
        Runat="server" />
    <br /><br />
    <asp:Label
        id="lblLastName"
        Text="Last Name:"
        AssociatedControlID="txtLastName"
        Runat="server" />
    <br />
    <asp:TextBox
        id="txtLastName"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Label font

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Server Controls - Basics</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>ASP.NET Server Controls</h1>
    <h2>Basics</h2>
     <asp:Label ID="lblTime" runat="server" OnInit="lblTime_Init"></asp:Label>
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
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 lblTime_Init(object sender, EventArgs e)
   {
      lblTime.Font.Name = "Verdana";
      lblTime.Font.Size = 20;
      lblTime.Font.Underline = true;
      lblTime.Font.Bold = true;
      lblTime.Font.Italic = true;
      lblTime.Font.Overline = true;
      lblTime.Font.Strikeout = true;
      lblTime.Text = DateTime.Now.ToString() + ". Font Name: " + lblTime.Font.Name;
   }
}


Label hot key

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Label HotKeys</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container">
    <h1>Using AccessKey for Controls</h1>
    <asp:Label ID="labName" runat="server" 
         AccessKey="N" AssociatedControlID="txtName" Text="<u>N</u>ame"></asp:Label>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
        <asp:Label ID="labMonth" runat="server" 
         AccessKey="M" AssociatedControlID="drpMonth" Text="<u>M</u>onth"></asp:Label>
    <asp:DropDownList ID="drpMonth" runat="server">
        <asp:ListItem>January</asp:ListItem>
        <asp:ListItem>February</asp:ListItem>
         <asp:ListItem>March</asp:ListItem>
    </asp:DropDownList>
    </div>
    </form>
</body>
</html>


Set Label through code (VB)

Page Language = "VB"
<%@ Page Language="VB" %>
<script runat="server">
   sub Page_Load(obj as object, e as eventargs)
      lblMessage.Text = "Welcome to ASP.NET!"
   end sub   
</script>
<html>
  <body>
    <asp:Label id="lblMessage" runat="server"/>
  </body>
</html>


Set text to Label (C#)

Page Language="C#"
<%@ Page Language="C#" %>
<script runat="server">
   void Page_Load(Object obj, EventArgs e) {
      lblMessage.Text = "Welcome to ASP.NET!";
   }
</script>
<html><body>
   <asp:Label id="lblMessage" runat="server"/>
</body></html>


Use asp:Label to display text message (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
   sub Page_Load(obj as object, e as eventargs)
      lblMessage.Text = "This is my first exercise!"
   end sub   
</script>
<html><body>
   <asp:Label id="lblMessage" runat="server"/>
</body></html>


Use of AssociatedControlID property on labels to bind a particular label to an input control

<%@ Page language="C#" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Label For</title>
</head>
<body>
    <div id="pageContent">
        <form runat="server" id="MainForm">
            <asp:Label ID="Label1" runat="server" Text="This label has no associated control"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" />
            <br />
            <asp:Label ID="Label2" runat="server" Text="This label is associated with the textbox"
                 AssociatedControlID="TextBox2"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" />
        </form>
    </div>
</body>
</html>


Using the Label server control to provide hot-key functionality

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Label Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
            <asp:Label ID="Label1" 
                       Runat="server" 
                       AccessKey="N" 
                       AssociatedControlID="Textbox1">User<u>n</u>ame
            </asp:Label>
            <asp:Textbox ID="TextBox1" Runat="server"></asp:Textbox>
            <asp:Label ID="Label2" 
                       Runat="server" 
                       AccessKey="P" 
                       AssociatedControlID="Textbox2"><u>P</u>assword
            </asp:Label>
            <asp:Textbox ID="TextBox2" Runat="server"></asp:Textbox>
            <asp:Button ID="Button1" Runat="server" Text="Submit" />
    </form>
</body>
</html>