ASP.NET - Contrôle TextBox

Le contrôle TextBox est utilisé pour créer un champ de texte où l'utilisateur peut saisir du texte.

Contrôle TextBox

Le contrôle TextBox est utilisé pour créer un champ de texte où l'utilisateur peut saisir du texte.

Les attributs du contrôle TextBox sont décrits dans notre Manuel de référence TextBox

L'exemple suivant montre certains attributs que vous pourriez utiliser dans le contrôle TextBox :

<html>
<body>
<form runat="server">
Un TextBox de base :
<asp:TextBox id="tb1" runat="server" />
<br /><br />
Un TextBox de mot de passe :
<asp:TextBox id="tb2" TextMode="password" runat="server" />
<br /><br />
Un TextBox avec du texte :
<asp:TextBox id="tb4" Text="Hello World!" runat="server" />
<br /><br />
Un TextBox multiligne :
<asp:TextBox id="tb3" TextMode="multiline" runat="server" />
<br /><br />
Un TextBox avec une hauteur :
<asp:TextBox id="tb6" rows="5" TextMode="multiline"
runat="server" />
<br /><br />
Un TextBox avec une largeur :
<asp:TextBox id="tb5" columns="30" runat="server" />
</form>
</body>
</html>

显示这个例子

Ajouter un script

Lorsque le formulaire est soumis, le contenu et les paramètres du contrôle TextBox peuvent être modifiés par le script serveur. Le formulaire peut être soumis en cliquant sur un bouton ou en modifiant la valeur du contrôle TextBox.

Dans l'exemple suivant, nous avons déclaré un contrôle TextBox, un contrôle Button et un contrôle Label dans un fichier .aspx. Lorsque le bouton de soumission est déclenché, la sous-routine submit est exécutée. La sous-routine submit écrit un texte au contrôle Label :

<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>

显示这个例子

在下面的例子中,我们在一个 .aspx 文件中声明了一个 TextBox 控件和一个 Label 控件。当您更改了 TextBox 中的值,并且在 TextBox 外单击时,change 子例程就会被执行。change 子例程会向 Label 控件写一条文本:

<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>

显示这个例子