Control.EnsureChildControls Método

Definição

Determinará se o controle de servidor contiver controles filho.Determines whether the server control contains child controls. Se ele não contiver, ele criará controles filho.If it does not, it creates child controls.

protected:
 virtual void EnsureChildControls();
protected virtual void EnsureChildControls ();
abstract member EnsureChildControls : unit -> unit
override this.EnsureChildControls : unit -> unit
Protected Overridable Sub EnsureChildControls ()

Exemplos

O exemplo a seguir usa o EnsureChildControls método para garantir que o controle do servidor atual tenha controles filho.The following example uses the EnsureChildControls method to ensure that the current server control has child controls. Em seguida, ele obtém ou define uma Text propriedade para um TextBox controle Web filho no objeto do controle de servidor atual ControlCollection .It then gets or sets a Text property for a child TextBox Web control in the current server control's ControlCollection object.

Importante

Este exemplo tem uma caixa de texto que aceita a entrada do usuário, que é uma possível ameaça à segurança.This example has a text box that accepts user input, which is a potential security threat. Por padrão, as páginas da Web do ASP.NET validam que a entrada do usuário não inclui elementos de script ou HTML.By default, ASP.NET Web pages validate that user input does not include script or HTML elements. Para obter mais informações, consulte Visão geral de explorações de script.For more information, see Script Exploits Overview.

// Ensure the current control has children,
// then get or set the Text property.
 public int Value {
    get {
        this.EnsureChildControls();
        return Int32.Parse(((TextBox)Controls[1]).Text);
    }
    set {
        this.EnsureChildControls();
        ((TextBox)Controls[1]).Text = value.ToString();
    }
 }

' Ensure the current control has children,
' then get or set the Text property.

Public Property Value() As Integer
   Get
      Me.EnsureChildControls()
      Return Int32.Parse(CType(Controls(1), TextBox).Text)
   End Get
   Set
      Me.EnsureChildControls()
      CType(Controls(1), TextBox).Text = value.ToString()
   End Set
End Property


Comentários

Esse método primeiro verifica o valor atual da ChildControlsCreated propriedade.This method first checks the current value of the ChildControlsCreated property. Se esse valor for false , o CreateChildControls método será chamado.If this value is false, the CreateChildControls method is called.

O EnsureChildControls método é normalmente usado em controles de composição, que são controles que usam controles filho para algumas ou todas as suas funcionalidades.The EnsureChildControls method is typically used in composite controls, which are controls that use child controls for some or all their functionality. O EnsureChildControls método é chamado para verificar se os controles filho foram criados e estão prontos para processar a entrada, para executar a vinculação de dados ou para executar outras tarefas.The EnsureChildControls method is called in order to make sure that child controls have been created and are ready to process input, to perform data binding, or to perform other tasks.

O GridView controle é um exemplo de um controle composto.The GridView control is an example of a composite control. Ele cria controles filho como Table ,, TableRow , TableCell Label e TextBox controles, que são usados para renderizar a tabela HTML que o GridView gera.It creates child controls such as Table, TableRow, TableCell, Label, and TextBox controls, which are used to render the HTML table that the GridView generates.

Na maioria dos casos, os desenvolvedores de controle de servidor personalizado não precisam substituir esse método.In most cases, custom server control developers do not have to override this method. Se você substituir esse método, use-o de forma semelhante ao comportamento padrão.If you do override this method, use it in a way similar to the default behavior.

Aplica-se a

Confira também