Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET textBox control


May 12, 2021 ASP.NET


Table of contents


ASP.NET Web Forms - TextBox controls

This section introduces you to ASP.NET the use of TextBox controls.

TextBox controls are used to create text boxes that users can enter text.


TextBox controls

TextBox controls are used to create text boxes that users can enter text.

The features and properties of textBox controls are listed on our WebForms control reference manual page.

The following example demonstrates some of the properties of the TextBox control that you might use:

<html>
<body>

<form runat="server">

A basic TextBox:
<asp:TextBox id="tb1" runat="server" />
<br /><br />

A password TextBox:
<asp:TextBox id="tb2" TextMode="password" runat="server" />
<br /><br />

A TextBox with text:
<asp:TextBox id="tb4" Text="Hello World!" runat="server" />
<br /><br />

A multiline TextBox:
<asp:TextBox id="tb3" TextMode="multiline" runat="server" />
<br /><br />

A TextBox with height:
<asp:TextBox id="tb6" rows="5" TextMode="multiline"
runat="server" />
<br /><br />

A TextBox with width:
<asp:TextBox id="tb5" columns="30" runat="server" />

</form>

</body>
</html>

Demo Examples . . .

Add a script

When a form is submitted, the contents and settings of the TextBox control may be modified by the server script. Forms can be submitted at the click of a button or when the user modifies the value of the TextBox control.

In the following example, we .aspx a TextBox control, a Button control, and a Label control in the file. W hen the submit button is triggered, the submit sub-routine is executed. The submit sub-routine writes a line of text to the Label control:

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Your name is " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Demo Examples . . .

In the following example, we .aspx a TextBox control and a Label control in the file. T he change sub-routine is executed when you modify the values in TextBox and click (or tab) outside the TextBox. The change sub-routine writes a line of text to the Label control:

<script runat="server">
Sub change(sender As Object, e As EventArgs)
lbl1.Text="You changed text to " & txt1.Text
End Sub
</script>

<html>
<body>

<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server"
text="Hello World!"
ontextchanged="change" autopostback="true"/>
<p><asp:Label id="lbl1" runat="server" /></p>
</form>

</body>
</html>

Demo Examples . . .