Literal.AddParsedSubObject(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
通知 ControlCollection 控制項已剖析 XML 或 HTML 項目,並將該項目加入至控制項的 Literal 中。
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
不是 LiteralControl 型別。
範例
下列程式碼範例示範如何在自訂伺服器控制項中覆寫 AddParsedSubObject 方法,以便在剖析的物件為 Literal 時,一律將 Text 屬性設定為剖析物件的 Text
屬性,否則為空白 string
。
<%@ 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 LiteralControl - AddParsedSubObject - C# Example</title>
<script runat="server">
void Button1_Click(Object sender, EventArgs e)
{
Literal1.Text = "Welcome to ASP.NET!";
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom LiteralControl - AddParsedSubObject - C# Example</h3>
<aspSample:CustomLiteralAddParsedSubObject
id="Literal1"
runat="server"
text="Literal Text" />
<br /><br />
<asp:Button
id="Button1"
Text="Change"
OnClick="Button1_Click"
runat="server"/>
</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 LiteralControl - AddParsedSubObject - VB.Net Example</title>
<script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs)
Literal1.Text = "Welcome to ASP.NET!"
End Sub
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom LiteralControl - AddParsedSubObject - VB.Net Example</h3>
<aspSample:CustomLiteralAddParsedSubObject id="Literal1"
runat="server" text="Literal Text" />
<br /><br />
<asp:Button id="Button1"
Text="Change"
OnClick="Button1_Click"
runat="server"/>
</form>
</body>
</html>
using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class CustomLiteralAddParsedSubObject : System.Web.UI.LiteralControl
{
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 CustomLiteralAddParsedSubObject
Inherits System.Web.UI.WebControls.Literal
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