Control.AddParsedSubObject(Object) 方法


通知服务器控件,分析了一个元素(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.

 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)



一个 Object,表示已经过分析的元素。An 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 对象中,itemsIf they are, they are added to an ArrayList object, items. 调用重写的 CreateChildControls 方法时,它将循环访问 ArrayList 并将其中的每个对象添加到自定义服务器控件的 ControlCollectionWhen 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 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述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;

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) 

   // Override 'CreateChildControls'. 
   protected override void CreateChildControls()
      System.Collections.IEnumerator myEnumerator = items.GetEnumerator();
' 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
      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.