Control.AddParsedSubObject(Object) Control.AddParsedSubObject(Object) Control.AddParsedSubObject(Object) Control.AddParsedSubObject(Object) Method

定義

通知伺服器控制項,XML 或 HTML 項目已剖析,並將項目加入伺服器控制項的 ControlCollection 物件中。Notifies the server control that an element, either XML or HTML, was parsed, and adds the element to the server control's ControlCollection object.

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 Object Object

代表已剖析項目的 ObjectAn Object that represents the parsed element.

實作

範例

下列範例是自訂伺服器控制項,它會使用AddParsedSubObject方法來判斷在此控制項的開頭和結束記號之間宣告的元素是否TextBox為 Web 服務器控制項。The following example is a custom server control that uses the AddParsedSubObject method to determine if elements declared between the opening and closing tags of this control are TextBox Web server controls. 如果是,則會加入至ArrayList items物件。If they are, they are added to an ArrayList object, items. 呼叫覆寫CreateChildControls的方法時,它會ArrayList逐一查看,並將其中的每個物件ControlCollection加入至自訂伺服器控制項的。When the overridden CreateChildControls method is called, it iterates through the ArrayList and adds each object in it to the ControlCollection of the custom server control.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see 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伺服器控制項的物件。Unless you override it, this method automatically adds LiteralControl objects to the server control's ControlCollection object. 此集合可透過Control.Controls屬性存取。This collection is accessible through Control.Controls property.

適用於

另請參閱