TextBox.AddParsedSubObject(Object) Metodo

Definizione

Sottoposto a override per consentire solo l'aggiunta di controlli letterali come la proprietà 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)

Parametri

obj
Object

Oggetto Object che rappresenta l'elemento sottoposto ad analisi.

Eccezioni

obj non è di tipo LiteralControl.

Esempio

Nell'esempio di codice seguente viene illustrato come eseguire l'override del AddParsedSubObject metodo in un controllo server personalizzato in modo che imposti sempre la Text proprietà sulla proprietà dell'oggetto Text analizzato se l'oggetto analizzato è un Literal controllo e genera un'eccezione in caso contrario.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite 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

Si applica a

Vedi anche