ASP .NET - The TextBox Control

The TextBox//文本框 control//控件 is used to create a text box//文本框 where the user can input text//输入文本.


The TextBox Control//文本框控件

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

The example below demonstrates//演示 some of the attributes//属性 you may use with the TextBox control:

Example

ASPX Source:

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



Output Result:

A basic TextBox:

A password TextBox:

A TextBox with text:

A multiline TextBox:

A TextBox with height:

A TextBox with width:
 


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


Add a Script//脚本

The contents//内容 and settings//设置 of a TextBox control may be changed by server scripts//服务器端脚本 when a form is submitted//提交. A form can be submitted by clicking on a button or when a user changes the value in the TextBox control.

In the following example we 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//子程序 writes a 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>

Example

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

Output Result:
Enter your name:

 


if you input "shaohai" in the textbox;

the Output Result:
Enter your name:

Your name is shaohai

 

In the following example we declare one TextBox control and one Label//标签 control in an .aspx file. When you change the value in the TextBox and either click outside the TextBox or press the Tab key//按下TAB键, the change subroutine is executed. The submit subroutine writes a 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>

Example

ASPX Source:

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



Output Result:
Change text

 

if you change the content to"hello,shaohai!"

the output result:

Change text

You changed text to Hello shaohai!

 

posted on 2007-01-17 16:27  改变热爱  阅读(256)  评论(0)    收藏  举报

导航