Share via


方法 : HTML サーバー コントロール プロパティをプログラムで設定する

更新 : 2007 年 11 月

HTML サーバー コントロールには、わずかに異なる 2 つの種類があります。フォームで最も一般的に使用される HTML 要素は、HtmlInputTextHtmlInputButtonHtmlTable の各コントロールなどの個別の HTML サーバー コントロールとして使用できます。これらの HTML サーバー コントロールは、HTML 属性に直接対応する、各コントロール固有のプロパティを公開します。ただし、すべての HTML 要素はコントロールに変換できます。その場合、TagName、Visible、InnerHTML などの基本クラスのプロパティを含む HtmlGenericControl になります。

HTML サーバー コントロールのプロパティを設定するには

  • ほかのオブジェクトと同様に、プロパティ名を取得または設定します。すべてのプロパティは、文字列または整数です。

    プロパティ名を設定する例を次に示します。

    Dim TotalCost As Integer
    myAnchor.HRef = "https://www.microsoft.com"
    Text1.MaxLength = 20
    Text1.Text = String.Format("{0:$###}", TotalCost)
    Span1.InnerHtml = "You must enter a value for Email Address."
    
    myAnchor.HRef = "https://www.microsoft.com";
    Text1.MaxLength = 20;
    Text1.Text = string.Format("{0:$####}", TotalCost);
    Span1.InnerHtml = "You must enter a value for Email Address.";
    

属性の設定

すべての HTML サーバー コントロールは、Attributes コレクションをサポートしています。これにより、コントロールのすべての属性に直接アクセスできます。これは、個別のプロパティとして公開されていない属性を使用するときに便利です。

コントロールの属性に直接アクセスするには

  • Add、Remove、Clear、Count など、コントロールの Attributes コレクションのプロパティとメソッドを使用します。Keys プロパティは、コントロール内のすべての属性の名前を含むコレクションを返します。Attributes コレクションを使用する各種の方法を次の例に示します。

        ' Adds new attribute.
        Text1.Attributes.Add("bgcolor", "red")
        ' Removes one attribute.
        Text1.Attributes.Remove("maxlength")
        ' Removes all attributes, clearing all properties.
        'Text1.Attributes.Clear()
        ' Creates comma-delimited list of defined attributes
        Dim strTemp As String = ""
        Dim key As String
        For Each key In Text1.Attributes.Keys
            strTemp &= Text1.Attributes(key) & ", "
        Next
    End Sub
    
    // Adds a new attribute.
    Text1.Attributes.Add("bgcolor", "red");
    // Removes one attribute.
    Text1.Attributes.Remove("maxlength");
    // Removes all attributes, clearing all properties.
    Text1.Attributes.Clear();
    // Creates comma-delimited list of defined attributes
    string strTemp = "";
    foreach (string key in Text1.Attributes.Keys)
    {
        strTemp += Text1.Attributes[key] + ", ";
    }
    

参照

処理手順

方法 : ASP.NET サーバー コントロールのプロパティを設定する

その他の技術情報

プログラムによる ASP.NET サーバー コントロール プロパティの設定