Asp.net programmers make extensive use of the TextBox control to let the user view or enter large amount of text. In this article, we introduce TextBox control – an input control in Asp.net:-
TextBox control – an input control in Asp.net
A TextBox Control is a rectangular box acts as an Input Control used by user to input some text/value. It is a valuable piece of the ASP.NET system.
The basic premise of the TextBox control is accepting user input. Text box controls allow entering text on a form at runtime. By default, it takes a single line of text, however, you can make it accept multiple texts and even add scroll bars to it.
Basic syntax of TextBox control:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
Adding TextBox Control to WebForm
- Create new Asp.net web application with a Default.aspx web form.
- Drag & Drop TextBox control from the Toolbox to web page.
- Set properties for the control in Properties task pane.
Properties of TextBox Control
The following are some of the commonly used properties of the TextBox control:
- ID : ID is the identification name of the control.
- BackColor: Sets the background color of the control.
- BorderColor: Sets the color of the border.
- BorderStyle: Sets the style of the border.
- BorderWidth: Sets the width of the border.
- Columns: Sets the width of the the control.
- Enabled: true/false (used to enable and disable the control).
- ForeColor: Sets the text color within the control.
- Height: Gets or sets the height of the control.
- Text: Display text within the control.
- Visible: true/false (used to check the visibility of the control).
- AutoPostBack: AutoPostBack property indicate whether an automatic postback to the server occurs when specified control loses it’s focus.
- Wrap: Indicates whether a multiline text box control automatically wraps words to the beginning of the next line when necessary.
Some Important Properties of TextBox Control
Events of TextBox Control
TextChanged Event
The TextChanged event is raised when the content of the text box changes between posts to the server. This event does not post the page back to the server unless the AutoPostBack property is set to true.
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
Example:
For Example, a user confirms his/her email address.
Design Webform :- Here, we have two text boxes in which user enters his/her email address & confirm it.
HTML View:
<div> <br /> <asp:Label ID="Label1" runat="server" Text="Email" Font-Size="18pt" Font-Bold="True" ForeColor="Red"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" style="margin-left: 112px" BackColor="Black" BorderColor="Red" BorderStyle="Inset" BorderWidth="5px" ForeColor="White" OnTextChanged="TextBox1_TextChanged" Width="181px"></asp:TextBox> <br /><br /> <asp:Label ID="Label2" runat="server" Text="Confirm Email" Font-Size="18pt" Font-Bold="True" ForeColor="Red"></asp:Label> <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 21px" BackColor="Black" BorderColor="Red" BorderStyle="Inset" BorderWidth="5px" ForeColor="White" Width="181px"></asp:TextBox> <br /> <br /> </div>
Generate Code :- To generate the command event handler, double click on textbox control.
protected void TextBox1_TextChanged(object sender, EventArgs e) { TextBox2.Text = TextBox1.Text; }
Output :- Press “F5” or “Ctrl + F5”. Enter your E-mail address .