共用方式為


TextBox.Text 屬性

定義

取得或設定 TextBox 控制項的文字內容。

public:
 virtual property System::String ^ Text { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.EncodedInnerDefaultProperty)]
public virtual string Text { get; set; }
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.EncodedInnerDefaultProperty)]
[System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)]
public virtual string Text { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.EncodedInnerDefaultProperty)>]
member this.Text : string with get, set
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.EncodedInnerDefaultProperty)>]
[<System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)>]
member this.Text : string with get, set
Public Overridable Property Text As String

屬性值

顯示於 TextBox 控制項中的文字。 預設為空字串 ("")。

實作

屬性

範例

下列程式代碼範例示範如何使用 TextBox 控件來取得用戶輸入。 當使用者按兩下 [ 新增 ] 按鈕時,就會顯示文字框中輸入的值總和。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html>
<html>

<head>
    <title>TextBox Example</title>
    <script runat="server">
      protected void AddButton_Click(Object sender, EventArgs e)
      {
         int Answer;
         Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text);
         AnswerMessage.Text = Answer.ToString();
      }
    </script>
</head>
<body>
  <form id="form1" runat="server">
    <h3> TextBox Example </h3>
    <p>
        <asp:TextBox ID="Value1" Columns="2" MaxLength="3" Text="1" runat="server"/>
        +
        <asp:TextBox ID="Value2" Columns="2" MaxLength="3" Text="1" runat="server"/>
        =
        <asp:Label ID="AnswerMessage" runat="server"/>
    </p>
    <p>
        <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/>
    </p>
    <p>
        <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1"
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"
             runat="server"/>
        <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2"
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"
             runat="server"/>
        <asp:RangeValidator ID="Value1RangeValidator" ControlToValidate="Value1"
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
             runat="server"/>
        <asp:RangeValidator ID="Value2RangeValidator" ControlToValidate="Value2"
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
             runat="server"/>
    </p>
  </form>
</body>
</html>
<%@ Page Language="vb" AutoEventWireup="false" %>

<!DOCTYPE html>
<html>

<head>
    <title>TextBox Example</title>
    <script runat="server">
        Protected Sub AddButton_Click(sender As Object, e As EventArgs)
            Dim Answer As Integer
            Answer = Convert.ToInt32(Value1.Text) + Convert.ToInt32(Value2.Text)
            AnswerMessage.Text = Answer.ToString()
        End Sub
    </script>
</head>
<body>
  <form id="form1" runat="server">
    <h3> TextBox Example </h3>
    <p>
        <asp:TextBox ID="Value1" Columns="2" MaxLength="3" Text="1" runat="server"/>
        +
        <asp:TextBox ID="Value2" Columns="2" MaxLength="3" Text="1" runat="server"/>
        =
        <asp:Label ID="AnswerMessage" runat="server"/>
    </p>
    <p>
        <asp:Button ID="AddButton" Text="Add" OnClick="AddButton_Click" runat="server"/>
    </p>
    <p>
        <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1"
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"
             runat="server"/>
        <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2"
             ErrorMessage="Please enter a value.<br />" Display="Dynamic"
             runat="server"/>
        <asp:RangeValidator ID="Value1RangeValidator" ControlToValidate="Value1"
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
             runat="server"/>
        <asp:RangeValidator ID="Value2RangeValidator" ControlToValidate="Value2"
             Type="Integer" MinimumValue="1" MaximumValue="100" Display="Dynamic"
             ErrorMessage="Please enter an integer <br /> between than 1 and 100.<br />"
             runat="server"/>
    </p>
  </form>
</body>
</html>

備註

Text使用屬性來指定或決定控件中顯示的TextBox文字。 若要限制 控制項所接受的字元數,請設定 MaxLength 屬性。 如果您想要防止修改文字,請設定 ReadOnly 屬性。

當設定時,此屬性的值可以使用設計工具自動儲存到資源檔。 如需詳細資訊,請參閱 LocalizableAttributeASP.NET 全球化和當地語系化

轉譯文字框時,此屬性的內容會以 HTML 編碼。 例如,如果您將此屬性設定為 「<b Test/b>>」,瀏覽器將會以粗體顯示 「<b Test<</b>>」。,而不是 「Test」。

適用於

另請參閱