TextBox.AddParsedSubObject(Object) Méthode

Définition

Substitué pour permettre l'ajout des contrôles littéraux uniquement comme propriété Text.

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)

Paramètres

obj
Object

Object qui représente l’élément analysé.

Exceptions

obj n'est pas de type LiteralControl.

Exemples

L’exemple de code suivant montre comment remplacer la AddParsedSubObject méthode dans un contrôle serveur personnalisé afin qu’elle définit toujours la propriété sur la Text propriété de Text l’objet analysé si l’objet analysé est un Literal contrôle et lève une exception sinon.

Important

Cet exemple comprend une zone de texte qui accepte une entrée d'utilisateur, ce qui constitue une menace potentielle pour la sécurité. Par défaut, les pages web ASP.NET vérifient que l’entrée d’utilisateur n’inclut pas de script ou d’éléments HTML. Pour plus d’informations, consultez Vue d’ensemble des attaques de script.

<%@ 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 TextBox - AddParsedSubObject - C# Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom TextBox - AddParsedSubObject - C# Example</h3>
            
            <aspSample:CustomTextBoxAddParsedSubObject 
              id="TextBox1" 
              runat="server">Hello World!
            </aspSample:CustomTextBoxAddParsedSubObject>
            
        </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 TextBox - AddParsedSubObject - VB.NET Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom TextBox - AddParsedSubObject - VB.NET Example</h3>
            
            <aspSample:CustomTextBoxAddParsedSubObject id="TextBox1" 
             runat="server">Hello World!</aspSample:CustomTextBoxAddParsedSubObject>
        </form>
    </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomTextBoxAddParsedSubObject : System.Web.UI.WebControls.TextBox
  {
    protected override void AddParsedSubObject(object obj)
    {
      // If the object is a LiteralControl, then set this control's Text property.
      if (obj is System.Web.UI.LiteralControl) 
      {
        this.Text = ((System.Web.UI.LiteralControl)obj).Text;
      }
      else 
      {
        throw new System.Web.HttpException("You cannot have a child control of type " + obj.GetType().Name.ToString());
      }
    }
  }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomTextBoxAddParsedSubObject
        Inherits System.Web.UI.WebControls.TextBox

        Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)

            ' If the object is a LiteralControl, then set this control's Text property.
            If TypeOf obj Is System.Web.UI.LiteralControl Then
                Me.Text = CType(obj, System.Web.UI.LiteralControl).Text
            Else
                Throw New System.Web.HttpException("You cannot have children controls of type " + obj.GetType().Name.ToString())
            End If
        End Sub
    End Class
End Namespace

S’applique à

Voir aussi