Export (0) Print
Expand All

Literal.AddParsedSubObject Method

This member overrides Control.AddParsedSubObject(Object), and more complete documentation might be available in that topic.

Notifies the Literal control that an XML or HTML element was parsed and adds that element to the ControlCollection of the control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

protected override void AddParsedSubObject(
	Object obj
)

Parameters

obj
Type: System.Object

An Object that represents the parsed element.

ExceptionCondition
HttpException

obj is not of type LiteralControl.

The following code example demonstrates how to override the AddParsedSubObject method in a custom server control so that it always sets the Text property to the parsed object's Text property, if the parsed object is a Literal, and to an empty string otherwise.

<%@ 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>
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 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);
        }
      }
    }
  }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft