Control.AddParsedSubObject(Object) Metodo

Definizione

Notifica al controllo server che un elemento, XML o HTML, è stato analizzato e aggiunge l'elemento all'oggetto ControlCollection del controllo server.

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)

Parametri

obj
Object

Oggetto Object che rappresenta l'elemento sottoposto ad analisi.

Esempio

L'esempio seguente è un controllo server personalizzato che usa il AddParsedSubObject metodo per determinare se gli elementi dichiarati tra i tag di apertura e chiusura di questo controllo sono TextBox controlli server Web. Se sono, vengono aggiunti a un ArrayList oggetto , items. Quando viene chiamato il CreateChildControls metodo sottoposto a override, esegue l'iterazione tramite ArrayList e aggiunge ogni oggetto al ControlCollection controllo server personalizzato.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.

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

Commenti

A meno che non venga eseguito l'override, questo metodo aggiunge LiteralControl automaticamente oggetti all'oggetto del ControlCollection controllo server. Questa raccolta è accessibile tramite Control.Controls la proprietà .

Si applica a

Vedi anche