ASP.NET Tutorial/ASP.net Controls/Label

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

Add style to asp:Label

   <source lang="csharp">

<%@ 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">
       <asp:Label CssClass="heading1" ID="Label1" runat="server" Text="This Label Uses heading1"></asp:Label>
       
This is sample unformatted text.
 
<asp:Label CssClass="heading2" ID="Label2" runat="server" Text="This Label Uses heading2"></asp:Label>
Here"s more unformatted text.

 
           This control uses the blockText style. This control uses the blockText style. This
           control uses the blockText style. This control uses the blockText style.
   </form>

</body> </html></source>


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

   <source lang="csharp">

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


Format a Label with CSS

   <source lang="csharp">

<%@ 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">
   <asp:Label
       id="lblFirst"
       Text="First Label"
       ForeColor="Red"
       BackColor="Yellow"
       BorderStyle="Solid"
       BorderWidth="2"
       BorderColor="red"
       Runat="server" />
   
<asp:Label id="lblSecond" Text="Second Label" CssClass="labelStyle" Runat="server" />
   </form>

</body> </html></source>


Highlight with regular expression

   <source lang="csharp">

<%@ 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>
<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 <script> 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 "" + m.Value + "";
  }

}</source>


Import properties of The Label control

   <source lang="csharp">

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">
   <asp:Label
       id="lblTime"
       Runat="server" />
   </form>

</body> </html></source>


Label controls are used to label the two TextBox controls.

   <source lang="csharp">

<%@ 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">
   <asp:Label
       id="lblFirstName"
       Text="First Name:"
       AssociatedControlID="txtFirstName"
       Runat="server" />
   
<asp:TextBox id="txtFirstName" Runat="server" />

<asp:Label id="lblLastName" Text="Last Name:" AssociatedControlID="txtLastName" Runat="server" />
<asp:TextBox id="txtLastName" Runat="server" />
   </form>

</body> </html></source>


Label font

   <source lang="csharp">

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

ASP.NET Server Controls

Basics

    <asp:Label ID="lblTime" runat="server" OnInit="lblTime_Init"></asp:Label>
   </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;
  }

}</source>


Label hot key

   <source lang="csharp">

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

Using AccessKey for Controls

   <asp:Label ID="labName" runat="server" 
        AccessKey="N" AssociatedControlID="txtName" Text="Name"></asp:Label>
   <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Label ID="labMonth" runat="server" AccessKey="M" AssociatedControlID="drpMonth" Text="Month"></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>
   </form>

</body> </html></source>


Set Label through code (VB)

   <source lang="csharp">

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></source>


Set text to Label (C#)

   <source lang="csharp">

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></source>


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

   <source lang="csharp">

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


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

   <source lang="csharp">

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

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

   <title>Label For</title>

</head> <body>

       <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" />
           
<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>

</body> </html></source>


Using the Label server control to provide hot-key functionality

   <source lang="csharp">

<%@ 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">Username
           </asp:Label>
           <asp:Textbox ID="TextBox1" Runat="server"></asp:Textbox>
           <asp:Label ID="Label2" 
                      Runat="server" 
                      AccessKey="P" 
                      AssociatedControlID="Textbox2">Password
           </asp:Label>
           <asp:Textbox ID="TextBox2" Runat="server"></asp:Textbox>
           <asp:Button ID="Button1" Runat="server" Text="Submit" />
   </form>

</body> </html></source>