ASP.NET Textbox Control


The TextBox control is used to create a text box where the user can input text.

C# Code

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

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Button1_Click(object sender, EventArgs e)
        {
            String value = TextBox1.Text;
            Label1.Text = "You entered :" + value;
        }
    }
}

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
    
    </div>
    </form>
</body>
</html>

Properties

Property Description
AutoCompleteType Specifies the AutoComplete behavior of a TextBox
AutoPostBack A Boolean value that specifies whether the control is automatically posted back to the server when the contents change or not. Default is false
CausesValidation Specifies if a page is validated when a Postback occurs
Columns The width of the textbox
MaxLength The maximum number of characters allowed in the textbox
ReadOnly Specifies whether or not the text in the text box can be changed
Rows The height of the textbox (only used if TextMode="Multiline")
runat Specifies that the control is a server control.  Must be set to "server"
Text The contents of the textbox
TextMode Specifies the behavior mode of a TextBox control (SingleLine, MultiLine or Password)
ValidationGroup The group of controls that is validated when a Postback occurs
Wrap A Boolean value that indicates whether the contents of the textbox should wrap or not
OnTextChanged The name of the function to be executed when the text in the textbox has changed

ASP.NET Control Standard Properties

AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
For a full description, go to Web Control Standard Attributes.

ASP.NET Web Control Standard Properties

AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
For a full description, go to Web Control Standard Attributes.

Example of Textbox Control

Declare one TextBox control, one Button control, and one Label control in an .aspx file. When the submit button is triggered, the submit subroutine is executed. The submit subroutine copies the contents of the textbox to the Label control.

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

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.BackColor = Color.Gray;
            TextBox1.ForeColor = Color.White;
            TextBox1.Text ="Welcome to Textbox Control Tutorial";
            TextBox1.TextMode = TextBoxMode.MultiLine;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            String value = TextBox1.Text;
            Label1.Text = value;
        }
    }
}

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Button Control </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
       <br />
       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
       <br />
    
    </div>
    </form>
</body>
</html>

Output


Share this article on