女女 - 2007-6-19 9:29:00
通知服务器控件某个元素(XML 或 HTML)已经过语法分析,并将该元素添加到服务器控件的 ControlCollection 对象。
命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)
语法
Visual Basic(声明)
Protected Overridable Sub AddParsedSubObject ( _
obj As Object _
)
Visual Basic(用法)
Dim obj As Object
Me.AddParsedSubObject(obj)
C#
protected virtual void AddParsedSubObject (
Object obj
)
C++
protected:
virtual void AddParsedSubObject (
Object^ obj
)
J#
protected void AddParsedSubObject (
Object obj
)
JScript
protected function AddParsedSubObject (
obj : Object
)
参数
obj
表示已经过语法分析的元素的 Object。
备注
除非重写此方法,否则此方法自动将 LiteralControl 对象添加到服务器控件的 ControlCollection 对象。此集合可通过 Control.Controls 属性访问。
示例
在下面的示例中,一个自定义服务器控件使用 AddParsedSubObject 方法来确定在该控件的开始和结束标记间声明的元素是否为 TextBox Web 服务器控件。如果是,则将它们添加到 ArrayList 对象,items。调用已重写的 CreateChildControls 方法时,它会循环访问 ArrayList,并将其中的每个对象添加到自定义服务器控件的 ControlCollection 中。
Visual Basic 复制代码
' 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);
}
}