开发学院

您的位置:首页>教程>正文

教程正文

ASP .NET - TextBox 控件

ASP .NET - TextBox 控件

    TextBox 控件用于创建用户可输入文本的文本框。

    TextBox 控件

    TextBox 控件用于创建用户可输入文本的文本框。

    TextBox 控件的属性列在我们的 TextBox 控件参考手册中。

    下面的例子演示了您可能在 TextBox 控件中使用到的一些属性:

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


    添加脚本

    当表单被提交时,TextBox 控件的内容和设置可通过服务器脚本进行修改。可通过点击一个按钮或当用户更改 TextBox 控件中的值对表单进行提交。

    在下面的例子中,我们在一个 .htmlx 文件中声明了一个 TextBox 控件、一个 Button 控件和一个 Label 控件。当提交按钮被触发时,submit 子例程就会被执行。submit 子例程会向 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>


    在下面的例子中,我们在一个 .htmlx 文件中声明了一个 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>