次の方法で共有


Control.AddParsedSubObject メソッド

サーバー コントロールに、XML または HTML の要素が解析されたことを通知し、その要素をサーバー コントロールの ControlCollection オブジェクトに追加します。

Protected Overridable Sub AddParsedSubObject( _
   ByVal obj As Object _)
[C#]
protected virtual void AddParsedSubObject(objectobj);
[C++]
protected: virtual void AddParsedSubObject(Object* obj);
[JScript]
protected function AddParsedSubObject(
   obj : Object);

パラメータ

  • obj
    解析された要素を表す Object

解説

オーバーライドしない限り、このメソッドはサーバー コントロールの ControlCollection オブジェクトに LiteralControl オブジェクトを自動的に追加します。このコレクションは、 Control.Controls プロパティによってアクセスできます。

使用例

[Visual Basic, C#, C++] カスタム サーバー コントロールの例を次に示します。このコントロールは、 AddParsedSubObject メソッドを使用して、このコントロールの開始タグと終了タグの間で宣言されている要素が TextBox Web サーバー コントロールかどうかを確認します。Web サーバー コントロールの場合、これらの要素は ArrayList オブジェクト items に追加されます。オーバーライドされた CreateChildControls メソッドが呼び出されると、 ArrayList が反復処理され、その中の各オブジェクトがカスタム サーバー コントロールの ControlCollection に追加されます。

 
' Custom ControlBuilder class. Interprets nested tag name "myitem" as a textbox.
Public Class MyControlBuilder
   Inherits ControlBuilder

   Public Overrides Function GetChildControlType(tagName As String, _
                             attributes As IDictionary) As Type
      If String.Compare(tagName, "myitem", True) = 0 Then
         Return GetType(TextBox)
      End If
      Return Nothing
   End Function
End Class

<ControlBuilderAttribute(GetType(MyControlBuilder))> Public Class MyControl
   Inherits Control
   ' Stores all the controls specified as nested tags.
   Private items As New ArrayList()

   ' This function is internally invoked by IParserAccessor.AddParsedSubObject(Object).
   Protected Overrides Sub AddParsedSubObject(obj As Object)
      If TypeOf obj Is TextBox Then
         items.Add(obj)
      End If
   End Sub

  ' Override 'CreateChildControls'.
   Protected Overrides Sub CreateChildControls()
      Dim myEnumerator As System.Collections.IEnumerator = items.GetEnumerator()
      While myEnumerator.MoveNext()
         Me.Controls.Add(CType(myEnumerator.Current, TextBox))
      End While
   End Sub
End Class

[C#] 
// Custom ControlBuilder class. Interprets nested tag name "myitem" as a textbox. 
public class MyControlBuilder : ControlBuilder 
{
   public override Type GetChildControlType(String tagName,
                                       IDictionary attributes)
   {
      if (String.Compare(tagName, "myitem", true) == 0) 
      {
         return typeof(TextBox);
      }
      return null;
   }
}

[ 
ControlBuilderAttribute(typeof(MyControlBuilder)) 
]
public class MyControl : Control
{
   // Store all the controls specified as nested tags.
   private ArrayList items = new ArrayList();
   
   // This function is internally invoked by IParserAccessor.AddParsedSubObject(Object).
   protected override void AddParsedSubObject(Object obj) 
   {
      if (obj is TextBox) 
      {
         items.Add(obj);
      }
   }

   // Override 'CreateChildControls'. 
   protected override void CreateChildControls()
   {
      System.Collections.IEnumerator myEnumerator = items.GetEnumerator();
      while(myEnumerator.MoveNext())
          this.Controls.Add((TextBox)myEnumerator.Current);
   }
}    

[C++] 
// Custom ControlBuilder class. Interprets nested tag name "myitem" as a textbox.
public __gc class MyControlBuilder : public ControlBuilder
{
public:
   Type* GetChildControlType(String* tagName, IDictionary* /*attributes*/)
   {
      if (String::Compare(tagName, S"myitem", true) == 0)
      {
         return __typeof(TextBox);
      }
      return 0;
   }
};

[
   ControlBuilderAttribute(__typeof(MyControlBuilder))
]
public __gc class MyControl : public Control
{
   // Store all the controls specified as nested tags.
private:
   ArrayList* items;

public:
   MyControl()
   {
      items = new ArrayList();
   }

   // This function is internally invoked by IParserAccessor.AddParsedSubObject(Object).
protected:
   void AddParsedSubObject(Object* obj)
   {
      if (dynamic_cast<TextBox*>(obj))
      {
         items->Add(obj);
      }
   }

   // Override 'CreateChildControls'.
   void CreateChildControls()
   {
      System::Collections::IEnumerator* myEnumerator = items->GetEnumerator();
      while(myEnumerator->MoveNext())
         this->Controls->Add(dynamic_cast<TextBox*>(myEnumerator->Current));
   }
};

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

必要条件

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

参照

Control クラス | Control メンバ | System.Web.UI 名前空間 | ControlCollection