ASP.NET

Example of Label and Textbox Control in ASP.NET

Programmingempire

This article discusses an Example of Label and Textbox Control in ASP.NET. Basically, Label and textBox are two basic Web Form controls that we use quite frequently 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 possesses some properties, and methods. Also, a web control also responds to the events. The following section describes important properties, methods, and events of Label and TextBox control.

Label Control in ASP.NET

Specifically, the Label class is available in System.Web.UI.WebControls namespace. As can be seen in the following example, we use the Label control to display text on a web page. Mostly, it has properties for customizing its appearance and style. However, we can manipulate these properties with code. Likewise, the important properties of Label control are Text, Width, Height, Font, BackColor, ForeColor, Style, CssClass, Attributes, and so on.

TextBox Control in ASP.NET

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

Code Example of Label and Textbox Control in ASP.NET demonstrating TextChanged Event

The following example shows how to change the font style of a label control when the user enters a particular text in a TextBox. Particularly, it has a Text property that represents the text displayed in the TextBox. While, the TextMode property enables us to display a SingleLine, password or MultiLine TextBox. Similarly, other useful properties of TextBox are Wrap, ReadOnly, Enabled, MaxLength, Rows, Visible, Width, Height, Enabled, and so on. Apart from these properties a TextBox control also has properties to customize appearance and style.

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

The following application shows that how to change the Font of a Label according to the text that the user enters in the TextBox.

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!";
            }
        }
    }
}

Output

Example of Label and Textbox Control in ASP.NET Showing TextChanged Event of the TextBox
Example of Label and Textbox Control in ASP.NET Showing TextChanged Event of the TextBox

Further Reading

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Methods of Array Class

Anonymous Functions in C#

Programs to Find Armstrong Numbers in C#

Matrix Multiplication in C#

One Dimensional and Two Dimensional Indexers in C#

Static Class Example in C#

Rotating an Array in C#

Generic IList Interface and its Implementation in C#

Recursive Binary search in C#

C# Practice Questions

Creating Navigation Window Application Using WPF in C#

Find Intersection Using Arrays

An array of Objects and Object Initializer

Performing Set Operations in LINQ

Using Quantifiers in LINQ

Data Binding Using BulletedList Control

Grouping Queries in LINQ

Generic Binary Search in C#

Understanding the Quantifiers in LINQ

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Examples of Query Operations using LINQ in C#

An array of Objects and Object Initializer

Language-Integrated Query (LINQ) in C#

How Data Binding Works in WPF

Examples of Connected and Disconnected Approach in ADO.NET

New Features in C# 9

IEnumerable and IEnumerator Interfaces

KeyValuePair and its Applications

C# Root Class – Object

Access Modifiers in C#

Learning Properties in C#

Learning All Class Members in C#

Examples of Extension Methods in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Understanding the Concept of Nested Classes in C#

LINQ To SQL Examples

A Beginner’s Tutorial on WPF in C#

Explaining C# Records with Examples

Everything about Tuples in C# and When to Use?

Creating Jagged Arrays in C#

Linear Search and Binary search in C#

Learning Indexers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Basic Examples

IEqualityComparer Interface

programmingempire

You may also like...