Web Pages Tutor...login
Web Pages Tutorial
author:php.cn  update time:2022-04-11 14:20:28

Web Forms TextBox



The TextBox control is used to create a text box into which the user can enter text.


TextBox Control

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

The properties and properties of the TextBox control are listed in our WebForms Controls Reference Manual page.

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

Example

<!DOCTYPE html>
<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="tb3" Text="Hello World!" runat="server" />
<br><br>

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

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

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

</form>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example


Add script

When the form is submitted, the content and settings of the TextBox control may Will be modified by server script. A form can be submitted by clicking a button or when the user modifies the value of a TextBox control.

In the following example, we declare a TextBox control, a Button control and a Label control in the .aspx file. When the submit button is triggered, the submit subroutine will be executed. The submit subroutine will write a line of text into the Label control:

Example

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

<!DOCTYPE html>
<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>

Run Example»

Click "Run Example" button to view the online example

In the following example, we declare a TextBox control and a Label control in the .aspx file. When you modify the value in the TextBox and click outside the TextBox (or press the Tab key), the change subroutine is executed. The change subroutine will write a line of text to the Label control:

Example

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

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

Run Example»

Click "Run instance" button to view the online instance


php.cn