Date:

Share:

Example of Label and Textbox Control in ASP.NET

Related Articles

This article discusses the example of label control and text boxes in ASP.NET. Basically, Label and textBox are two basic Web Form controls that we use quite often in a web application. While the label control displays static text, the TextBox control allows us to enter the data.

In particular, each web control has several characteristics and methods. Also, internet control also responds to events. The following section describes important features, methods, and events of label and TextBox control.

Label control in ASP.NET

Specifically, the Label class is available in the System.Web.UI.WebControls namespace. As you can see in the following example, we use the label control to display text on a web page. Most often, it has features for customizing its look and style. However, we can manipulate these properties with code. Also, the important characteristics of label control are text, width, height, font, back color, ForeColor, style, CssClass, attributes and so on.

Control a text box in ASP.NET

Like Label, TextBox control is also available in the System.Web.UI.WebControls namespace. This allows the user to enter input text.

Example code and text box control code in ASP.NET that demonstrates a TextChanged event

The following example shows how to change the font style of a label control when the user enters a specific text in a text box. In particular, it has a text property that represents the text displayed in the text box. Admittedly, the TextMode property allows us to display SingleLine, Password or MultiLine TextBox. Similarly, other useful features of TextBox are Wrap, ReadOnly, Enabled, MaxLength, Rows, Visible, Width, Height, Enabled, and so on. Apart from these features the TextBox control also has customizable features of look and style.

In order to respond to changes in text, the TextBox control has a The text has changed case. This event occurs in case of a change in the text when the control publishes to the server

The following app shows how to change the font of a label according to the text that the user enters in the text box.

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LabelAndTextBoxDemo.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div> 
            <br />
            <br />
            <asp:Label ID="Label3" runat="server" Text="Label and TextBox Demo in ASP.NET"></asp:Label>
            <br /><br />
            <asp:Label ID="Label1" runat="server" Width="300" Text="Enter a Font Style"></asp:Label>
 
            <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"/>
            <br /><br />
            <asp:Label ID="Label2" runat="server" Text="Sample Text"></asp:Label>
        </div>
    </form>
</body>
</html>

WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LabelAndTextBoxDemo

    public partial class WebForm1 : System.Web.UI.Page
    
        protected void Page_Load(object sender, EventArgs e)
        
            Label3.Font.Size = FontUnit.XXLarge;
            Label3.BackColor = System.Drawing.Color.Coral;
            Label3.ForeColor = System.Drawing.Color.DarkMagenta;
        

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        
            String text = TextBox1.Text;
            if(text.Equals("Bold"))
            
                Label2.Font.Bold = true;
            
            else if (text.Equals("Italic"))
            
                Label2.Font.Italic = true;
            
            else if (text.Equals("Underline"))
            
                Label2.Font.Underline = true;
            
            else if (text.Equals("Overline"))
            
                Label2.Font.Overline = true;
            
            else
            
                Label1.Text = "Enter a valid font style!";
            
        
    

Productivity

Example of a label and text boxes control in ASP.NET that displays a TextChanged text box event

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles