Label.AddParsedSubObject(Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
通知控件某元素已经过分析,并将该元素添加到 Label 控件。
protected:
override void AddParsedSubObject(System::Object ^ obj);
protected override void AddParsedSubObject (object obj);
override this.AddParsedSubObject : obj -> unit
Protected Overrides Sub AddParsedSubObject (obj As Object)
参数
- obj
- Object
表示已分析元素的对象。
示例
下面的代码示例演示如何重写 AddParsedSubObject 自定义 Label 服务器控件中的方法,以便它始终将文本属性设置为已分析对象的文本属性(如果已分析的对象是一个 Literal,否则设置为空字符串)。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom Label - AddParsedSubObject - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom Label - AddParsedSubObject - C# Example</h3>
<aspSample:CustomLabelAddParsedSubObject
id="Label1" runat="server"
ToolTip="Microsoft Corp.">Microsoft Corp.</aspSample:CustomLabelAddParsedSubObject>
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom Label - AddParsedSubObject - VB.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom Label - AddParsedSubObject - VB.NET Example</h3>
<aspSample:CustomLabelAddParsedSubObject id="Label1" runat="server"
ToolTip="Microsoft Corp.">Microsoft Corp.</aspSample:CustomLabelAddParsedSubObject>
</form>
</body>
</html>
using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
public sealed class CustomLabelAddParsedSubObject : System.Web.UI.WebControls.Label
{
protected override void AddParsedSubObject(object obj)
{
// If the server control contains any child controls.
if (this.HasControls())
{
// Notify the base server control that an element, either XML or HTML,
// was parsed, and adds the element to the server control's
// ControlCollection object.
base.AddParsedSubObject(obj);
}
// Else the server control doesn't contain any child controls.
else
{
// If the parsed element is a LiteralControl.
if (obj is System.Web.UI.LiteralControl)
{
// Set the server control's Text property to the parsed element's Text value.
this.Text = ((System.Web.UI.LiteralControl)obj).Text;
}
// Else the parsed element is not a LiteralControl.
else
{
// If the server control has a value in the Text property.
string currentText = this.Text;
if (currentText.Length != 0)
{
// Set the server control's Text property to an empty string.
this.Text = System.String.Empty;
// Notify the base server control that a new LiteralControl was parsed,
// and adds the element to the server control's ControlCollection object.
base.AddParsedSubObject(new System.Web.UI.LiteralControl(currentText));
}
base.AddParsedSubObject(obj);
}
}
}
}
}
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CustomLabelAddParsedSubObject
Inherits System.Web.UI.WebControls.Label
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
' If the server control contains any child controls.
If Me.HasControls() Then
' Notify the base server control that an element, either XML or HTML,
' was parsed, and adds the element to the server control's
' ControlCollection object.
MyBase.AddParsedSubObject(obj)
' Else the server control doesn't contain any child controls.
Else
' If the parsed element is a LiteralControl.
If TypeOf obj Is System.Web.UI.LiteralControl Then
' Set the server control's Text property to the parsed element's Text value.
Me.Text = CType(obj, System.Web.UI.LiteralControl).Text
' Else the parsed element is not a LiteralControl.
Else
' If the server control has a value in the Text property.
Dim currentText As String = Me.Text
If currentText.Length <> 0 Then
' Set the server control's Text property to an empty string.
Me.Text = System.String.Empty
' Notify the base server control that a new LiteralControl was parsed,
' and adds the element to the server control's ControlCollection object.
MyBase.AddParsedSubObject(New System.Web.UI.LiteralControl(currentText))
End If
MyBase.AddParsedSubObject(obj)
End If
End If
End Sub
End Class
注解
从类派生自定义控件Label时,该方法AddParsedSubObject主要由控件开发人员使用。
如果输入对象是一个 LiteralControl,并且 Label 控件没有子控件,则输入对象用于设置 Text 控件的属性 Label 。 否则, AddParsedSubObject 将调用基 Control 类的方法,并将指定的对象添加到 Controls 集合中。