Control.FindControl Méthode
Définition
Recherche le contrôle serveur spécifié dans le conteneur d'attribution de noms en cours.Searches the current naming container for the specified server control.
Surcharges
FindControl(String) |
Recherche un contrôle serveur possédant le paramètre |
FindControl(String, Int32) |
Recherche le conteneur d'attribution de noms actuel d'un contrôle serveur avec l' |
FindControl(String)
Recherche un contrôle serveur possédant le paramètre id
spécifié dans le conteneur d'attribution de noms actuel.Searches the current naming container for a server control with the specified id
parameter.
public:
virtual System::Web::UI::Control ^ FindControl(System::String ^ id);
public virtual System.Web.UI.Control FindControl (string id);
abstract member FindControl : string -> System.Web.UI.Control
override this.FindControl : string -> System.Web.UI.Control
Public Overridable Function FindControl (id As String) As Control
Paramètres
- id
- String
Identificateur du contrôle à rechercher.The identifier for the control to be found.
Retours
Contrôle spécifié, ou null
s'il n'existe pas.The specified control, or null
if the specified control does not exist.
Exemples
L’exemple suivant définit un Button1_Click
Gestionnaire d’événements.The following example defines a Button1_Click
event handler. Lorsqu’il est appelé, ce gestionnaire utilise la FindControl méthode pour rechercher un contrôle avec une ID propriété de TextBox2
sur la page conteneur.When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2
on the containing page. Si le contrôle est trouvé, son parent est déterminé à l’aide de la Parent propriété et le du contrôle parent ID est écrit dans la page.If the control is found, its parent is determined using the Parent property and the parent control's ID is written to the page. Si TextBox2
est introuvable, « contrôle introuvable » est écrit dans la page.If TextBox2
is not found, "Control Not Found" is written to the page.
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é.This example has a text box that accepts user input, which is a potential security threat. 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.By default, ASP.NET Web pages validate that user input does not include script or HTML elements. Pour plus d’informations, consultez Vue d’ensemble des attaques de script.For more information, see Script Exploits Overview.
private void Button1_Click(object sender, EventArgs MyEventArgs)
{
// Find control on page.
Control myControl1 = FindControl("TextBox2");
if(myControl1!=null)
{
// Get control's parent.
Control myControl2 = myControl1.Parent;
Response.Write("Parent of the text box is : " + myControl2.ID);
}
else
{
Response.Write("Control not found");
}
}
Private Sub Button1_Click(sender As Object, MyEventArgs As EventArgs)
' Find control on page.
Dim myControl1 As Control = FindControl("TextBox2")
If (Not myControl1 Is Nothing)
' Get control's parent.
Dim myControl2 As Control = myControl1.Parent
Response.Write("Parent of the text box is : " & myControl2.ID)
Else
Response.Write("Control not found.....")
End If
End Sub
Remarques
Utilisez FindControl pour accéder à un contrôle à partir d’une fonction dans une page code-behind, pour accéder à un contrôle situé à l’intérieur d’un autre conteneur, ou dans d’autres circonstances où le contrôle cible n’est pas directement accessible à l’appelant.Use FindControl to access a control from a function in a code-behind page, to access a control that is inside another container, or in other circumstances where the target control is not directly accessible to the caller. Cette méthode ne trouvera un contrôle que si le contrôle est directement contenu dans le conteneur spécifié ; autrement dit, la méthode ne recherche pas dans une hiérarchie de contrôles dans les contrôles.This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls. Pour plus d’informations sur la recherche d’un contrôle lorsque vous ne connaissez pas son conteneur immédiat, consultez Comment : accéder aux contrôles serveur par ID.For information about how to find a control when you do not know its immediate container, see How to: Access Server Controls by ID.
Voir aussi
S’applique à
FindControl(String, Int32)
Recherche le conteneur d'attribution de noms actuel d'un contrôle serveur avec l'id
spécifié et un entier, spécifié dans le paramètre pathOffset
, qui facilite la recherche.Searches the current naming container for a server control with the specified id
and an integer, specified in the pathOffset
parameter, which aids in the search. Vous ne devez pas substituer cette version de la méthode FindControl.You should not override this version of the FindControl method.
protected:
virtual System::Web::UI::Control ^ FindControl(System::String ^ id, int pathOffset);
protected virtual System.Web.UI.Control FindControl (string id, int pathOffset);
abstract member FindControl : string * int -> System.Web.UI.Control
override this.FindControl : string * int -> System.Web.UI.Control
Protected Overridable Function FindControl (id As String, pathOffset As Integer) As Control
Paramètres
- id
- String
Identificateur du contrôle à rechercher.The identifier for the control to be found.
- pathOffset
- Int32
Nombre de contrôles en haut de la hiérarchie des contrôles de la page nécessaires pour atteindre un conteneur d'attribution de noms.The number of controls up the page control hierarchy needed to reach a naming container.
Retours
Contrôle spécifié, ou null
s'il n'existe pas.The specified control, or null
if the specified control does not exist.