JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Labels

You can use the Label class just as you use labels in Windows forms—to display text. You can change that text with the Text property in code if you want, but the user can't edit the text in a label control directly.

There's an example called Labels on the CD-ROM that puts a label to work; you can see this example in Figure 15.9. When you click the "Click me" button in this example, the code places the text "Hello!" in a label and adds a dashed border to the label, as shown in that figure.

Click To expand
Figure 15.9: The Labels example.

Here is WebForm1.aspx from the Labels example:

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="Labels.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content=" _
  <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server">
<asp:Label id=Label1 style="Z-INDEX: 101; LEFT: 250px;
POSITION: absolute; TOP: 79px" runat="server" Width="203px"
Height="118px" Font-Size="XX-Large"></asp:Label>
<asp:Button id=Button1 style="Z-INDEX: 102; LEFT: 127px;
POSITION: absolute; TOP: 107px" runat="server" Text="Click me">


And here is WebForm1.aspx.vb from the same example:

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Hello!"
        Label1.BorderStyle = BorderStyle.Dashed
    End Sub
End Class

In HTML, labels are supported by <span> elements; here's the one generated in the Labels example:

<span id="Label1" style="border-style:Dashed;font-size:XX-
Large;height:118px;width:203px;Z-INDEX: 101; LEFT: 250px; POSITION:
absolute; TOP: 79px">Hello!</span>
Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor