次の方法で共有


ParseChildrenAttribute の使用方法

ParseChildrenAttribute は、クラス レベルで適用する .NET Framework のメタデータ属性です。コントロールは、ASP.NET ページで宣言によってコントロールが使用される場合、ページ パーサーがコントロールのタグ内の入れ子になった要素を解釈する方法を ParseChildrenAttribute によって指定できます。ParseChildrenAttribute は、Boolean フラグとコントロールの既定のプロパティに対応する名前付き引数を使用して作成できます。この属性構文の詳細については、「ParseChildrenAttribute クラス」を参照してください。

ParseChildrenAttribute をコントロールに適用する方法を次の例に示します。

[ ParseChildren(ChildrenAsProperties = true)] 
// Or apply as [ ParseChildren(true)].
public class TemplatedFirstControl : Control, INamingContainer {...}
[Visual Basic]
<ParseChildren(ChildrenAsProperties := True)> _
Public Class TemplatedFirstControl
   Inherits Control
   Implements INamingContainer
   ...
End Class

ParseChildrenAttribute に設定されているコントロールのサンプルについては、「テンプレート コントロールのサンプル」を参照してください。TemplatedFirstControl を宣言によってページで使用する場合は、次の例に示すように、そのコントロール タグ内の子要素 (直接の子) は TemplatedFirstControl のプロパティに対応する必要があります。

<%-- FirstTemplate is a property of TemplatedFirstControl. --%>
<%-- Custom is the tag prefix for TemplatedFirstControl on the page as defined in the <%@ Register %> directive. --%>
<Custom:TemplatedFirstControl id = "First" Text= "The time on the server is "  runat=server>                         
   <FirstTemplate>
       <h3><font face="Verdana" color = "red"><%# Container.Text %> <%# Container.DateTime %>
       </font></h3>
    </FirstTemplate>      
</Custom:TemplatedFirstControl>

ParseChildrenAttribute の使用パターンを次の表に示します。

属性の使用法 説明
ParseChildrenAttribute(ChildrenAsProperties = true)

または

ParseChildren(true)

入れ子になった (子) 要素は、コントロールのプロパティに対応する必要があります。コントロール タグの間にその他の (非プロパティ) 要素やリテラル テキストが存在すると、パーサー エラーが生成されます。

例 : Repeater およびその他のデータ連結コントロール

ParseChildrenAttribute(ChildrenAsProperties = false)

または

ParseChildrenAttribute(false)

または

ParseChildrenAttribute は、コントロールに適用されません。

入れ子になった (子) 要素は、ASP.NET サーバー コントロールに対応する必要があります。ページ パーサーは、子コントロールを作成し、コントロールの IParserAccessor.AddParsedSubObject を呼び出します。AddParsedSubObject の既定の実装によって、子コントロールがコントロールの Controls コレクションに追加されます。ページ パーサーは、タグ間のリテラル テキストを LiteralControl のインスタンスとして解析します。

例 : Panel

ParseChildrenAttribute (ChildrenAsProperties = true, DefaultProperty = "PropertyName")

または

ParseChildrenAttribute(true, "PropertyName")

コントロールは、PropertyName と命名されたパブリック プロパティを定義する必要があります。入れ子になった (子) 要素は、PropertyName プロパティの子要素に対応する必要があります。

例 : HtmlTable、HtmlTableRow

サンプルについては、「ParseChildrenAttribute のサンプル」を参照してください。

ParseChildrenAttribute(false, "PropertyName") ページ パーサーは、この使用法を ParseChildrenAttribute(false) と同一と見なします。

メモ   System.Web.UI.WebControls.WebControl は、ParseChildren(true) とマークが付けられます。WebControl から派生したカスタム コントロールは、この属性のマーキングを継承します。別の引数を属性に再適用することによって、継承した属性をオーバーライドできます。

メモ   ParseChildrenAttribute に加えて ControlBuilderAttribute が適用された場合は、表で説明されている解析ロジックを変更できます。

参照

コントロール解析、ParseChildrenAttribute、およびコントロール ビルダ | ParseChildrenAttribute のサンプル