共用方式為


Control.AddParsedSubObject(Object) 方法

定義

通知伺服器控制項,XML 或 HTML 項目已剖析,並將項目加入伺服器控制項的 ControlCollection 物件中。

protected:
 virtual void AddParsedSubObject(System::Object ^ obj);
protected virtual void AddParsedSubObject (object obj);
abstract member AddParsedSubObject : obj -> unit
override this.AddParsedSubObject : obj -> unit
Protected Overridable Sub AddParsedSubObject (obj As Object)

參數

obj
Object

代表已剖析項目的 Object

範例

下列範例是自定義伺服器控制項,會使用 AddParsedSubObject 方法來判斷此控件的開頭和結尾標記之間宣告的專案是否為 TextBox Web 伺服器控制件。 如果是,則會將它們新增至 ArrayList 物件 items 呼叫覆寫 CreateChildControls 的方法時,它會逐一查看 ArrayList ,並將其中每個物件新增至 ControlCollection 自定義伺服器控制件的 。

重要

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

// 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);
   }
}    
' 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

備註

除非您覆寫它,否則這個方法會自動將物件新增 LiteralControl 至伺服器控件的 ControlCollection 物件。 此集合可透過 Control.Controls 屬性存取。

適用於

另請參閱