Suggerisci traduzione
 
Altri utenti hanno suggerito:

progress indicator
Nessun altro suggerimento.
Valuta il contenuto e lascia un commento
MSDN
MSDN Library
.NET Framework 4
System.Web.UI
Classe Control
Metodi Control
 Metodo FindControl (String)
Comprimi tutto/Espandi tutto Comprimi tutto
Visualizza contenuto:  affiancatoVisualizza contenuto: affiancato
.NET Framework Class Library
Control..::.FindControl Method (String)

Searches the current naming container for a server control with the specified id parameter.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Visual Basic
Public Overridable Function FindControl ( _
    id As String _
) As Control
C#
public virtual Control FindControl(
    string id
)
Visual C++
public:
virtual Control^ FindControl(
    String^ id
)
F#
abstract FindControl : 
        id:string -> Control 
override FindControl : 
        id:string -> Control 

Parameters

id
Type: System..::.String
The identifier for the control to be found.

Return Value

Type: System.Web.UI..::.Control
The specified control, or nullNothingnullptra null reference (Nothing in Visual Basic) if the specified control does not exist.

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. 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. For information about how to find a control when you do not know its immediate container, see How to: Access Server Controls by ID.

TopicLocation
Procedura: fare riferimento a contenuto di pagine master ASP.NETGenerazione di applicazioni Web ASP.NET
Procedura: fare riferimento a contenuto di pagine master ASP.NETCompilazione di applicazioni Web ASP.NET in Visual Studio

The following example defines a Button1_Click event handler. When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2 on the containing 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. If TextBox2 is not found, "Control Not Found" is written to the page.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

Visual Basic

   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

C#
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");
      }
}

.NET Framework

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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Riferimento a .NET Framework
Metodo Control..::.FindControl (String)

Cerca un controllo server nel contenitore dei nomi corrente con il parametro id specificato.

Spazio dei nomi:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Visual Basic
Public Overridable Function FindControl ( _
    id As String _
) As Control
C#
public virtual Control FindControl(
    string id
)
Visual C++
public:
virtual Control^ FindControl(
    String^ id
)
F#
abstract FindControl : 
        id:string -> Control 
override FindControl : 
        id:string -> Control 

Parametri

id
Tipo: System..::.String
Identificatore del controllo da ricercare.

Valore restituito

Tipo: System.Web.UI..::.Control
Il controllo specificato oppure nullNothingnullptrriferimento null (Nothing in Visual Basic) se il controllo specificato non esiste.

Utilizzare FindControl per accedere a un controllo da una funzione in una pagina code-behind, per accedere a un controllo che si trova in un altro contenitore o in altre circostanze dove il controllo di destinazione non è direttamente accessibile al chiamante. Questo metodo troverà un controllo solo se il controllo è contenuto direttamente nel contenitore specificato; ovvero, il metodo non esegue la ricerca in tutta la gerarchia dei controlli all'interno dei controlli. Per informazioni sull'individuazione di un controllo quando non si conosce il contenitore diretto relativo, vedere Procedura: accedere ai controlli server in base all'ID.

TopicLocation
Procedura: fare riferimento a contenuto di pagine master ASP.NETGenerazione di applicazioni Web ASP.NET
Procedura: fare riferimento a contenuto di pagine master ASP.NETCompilazione di applicazioni Web ASP.NET in Visual Studio

Nell'esempio che segue viene definito un gestore eventi Button1_Click. Quando viene richiamato, il gestore utilizza il metodo FindControl per localizzare un controllo con una proprietà ID di TextBox2 nella pagina che lo contiene. Se il controllo viene rilevato, il relativo elemento padre viene determinato tramite la proprietà Parent e il ID del controllo padre viene scritto nella pagina. Se non viene trovato TextBox2, nella pagina viene scritto il messaggio "Control Not Found".

Nota sulla sicurezzaNota sulla sicurezza

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 ulteriori informazioni, vedere Cenni preliminari sugli attacchi tramite script.

Visual Basic

   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

C#
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");
      }
}

.NET Framework

Supportato in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 o versione successiva, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (componenti di base del server non supportati), Windows Server 2008 R2 (componenti di base del server supportati con SP1 o versione successiva), Windows Server 2003 SP2

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.
Contenuti della community   Che cos'è Contenuti della community?
Aggiungi nuovo contenuto RSS  Annotazioni
Processing
© 2012 Microsoft. Tutti i diritti riservati. Condizioni per l'utilizzo | Marchi | Informativa sulla privacy
Page view tracker