次の方法で共有


TextBox.Text プロパティ

TextBox コントロールのテキストの内容を取得または設定します。

Public Overridable Property Text As String
[C#]
public virtual string Text {get; set;}
[C++]
public: __property virtual String* get_Text();public: __property virtual void set_Text(String*);
[JScript]
public function get Text() : String;public function set Text(String);

プロパティ値

TextBox コントロール内に表示するテキスト。既定値は空の文字列 ("") です。

解説

Text プロパティを使用して、 TextBox コントロールに表示するテキストを指定または確認します。コントロールの文字数を制限するには、 MaxLength プロパティを設定します。テキストが変更されないようにするには、 ReadOnly プロパティを設定します。

使用例

[Visual Basic, C#] Text プロパティを使用して、 TextBox コントロールに表示するテキストを指定する方法を次の例に示します。

 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html> 

<head>

   <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 runat="server">

      <h3> TextBox Example </h3>
















































      <table>
         <tr>
            <td colspan="5">
               Enter integer values into the text boxes. <br>
               Click the Add button to add the two values. <br>
               Click the Reset button to reset the text boxes.
            </td>
         </tr>
         <tr>
            <td colspan="5">
               &nbsp;
            </td>
         </tr>
         <tr align="center">
            <td>
               <asp:TextBox ID="Value1"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>
            </td>
            <td>
               + 
            </td>
            <td>
               <asp:TextBox ID="Value2"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>
            </td>
            <td>
               =
            </td>
            <td>
               
               <asp:Label ID="AnswerMessage"
                    runat="server"/>
            </td>
         </tr>
         <tr>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value1RequiredValidator"
                    ControlToValidate="Value1"
                    ErrorMessage="Please enter a value.<br>"
                    Display="Dynamic"
                    runat="server"/>
               <asp:RangeValidator
                    ID="Value1RangeValidator"
                    ControlToValidate="Value1"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br> between than 1 and 100.<br>"
                    Display="Dynamic"
                    runat="server"/>
            </td>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value2RequiredValidator"
                    ControlToValidate="Value2"
                    ErrorMessage="Please enter a value.<br>"
                    Display="Dynamic"
                    runat="server"/>
               <asp:RangeValidator
                    ID="Value2RangeValidator"
                    ControlToValidate="Value2"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br> between than 1 and 100.<br>"
                    Display="Dynamic"
                    runat="server"/>
            </td>
            <td>
               &nbsp
 
            </td
         </tr>
         <tr align="center">
            <td colspan="4">
               <asp:Button ID="AddButton"
                    Text="Add"
                    OnClick="AddButton_Click"
                    runat="server"/>
            </td>
            <td>
               &nbsp;
            </td>
         </tr>
      </table>


   </form>

</body>
</html>

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

<html> 

<head>

   <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 runat="server">

      <h3> TextBox Example </h3>
















































      <table>
         <tr>
            <td colspan="5">
               Enter integer values into the text boxes. <br>
               Click the Add button to add the two values. <br>
               Click the Reset button to reset the text boxes.
            </td>
         </tr>
         <tr>
            <td colspan="5">
               &nbsp;
            </td>
         </tr>
         <tr align="center">
            <td>
               <asp:TextBox ID="Value1"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>
            </td>
            <td>
               + 
            </td>
            <td>
               <asp:TextBox ID="Value2"
                    Columns="2"
                    MaxLength="3"
                    Text="1"
                    runat="server"/>
            </td>
            <td>
               =
            </td>
            <td>
               
               <asp:Label ID="AnswerMessage"
                    runat="server"/>
            </td>
         </tr>
         <tr>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value1RequiredValidator"
                    ControlToValidate="Value1"
                    ErrorMessage="Please enter a value.<br>"
                    Display="Dynamic"
                    runat="server"/>
               <asp:RangeValidator
                    ID="Value1RangeValidator"
                    ControlToValidate="Value1"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br> between than 1 and 100.<br>"
                    Display="Dynamic"
                    runat="server"/>
            </td>
            <td colspan="2">
               <asp:RequiredFieldValidator
                    ID="Value2RequiredValidator"
                    ControlToValidate="Value2"
                    ErrorMessage="Please enter a value.<br>"
                    Display="Dynamic"
                    runat="server"/>
               <asp:RangeValidator
                    ID="Value2RangeValidator"
                    ControlToValidate="Value2"
                    Type="Integer"
                    MinimumValue="1"
                    MaximumValue="100"
                    ErrorMessage="Please enter an integer <br> between than 1 and 100.<br>"
                    Display="Dynamic"
                    runat="server"/>
            </td>
            <td>
               &nbsp
 
            </td
         </tr>
         <tr align="center">
            <td colspan="4">
               <asp:Button ID="AddButton"
                    Text="Add"
                    OnClick="AddButton_Click"
                    runat="server"/>
            </td>
            <td>
               &nbsp;
            </td>
         </tr>
      </table>


   </form>

</body>
</html>

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

参照

TextBox クラス | TextBox メンバ | System.Web.UI.WebControls 名前空間 | MaxLength | ReadOnly