TemplateControl.LoadControl Metodo
Definizione
Overload
LoadControl(String) |
Carica un controllo Control da un file in base al percorso virtuale specificato.Loads a Control object from a file based on a specified virtual path. |
LoadControl(Type, Object[]) |
Carica un oggetto Control in base al tipo e ai parametri del costruttore specificati.Loads a Control object based on a specified type and constructor parameters. |
Commenti
Quando si carica un controllo in un controllo contenitore, il contenitore genera tutti gli eventi del controllo aggiunto fino a quando non viene rilevato l'evento corrente.When you load a control into a container control, the container raises all of the added control's events until it has caught up to the current event. Tuttavia, il controllo aggiunto non rileva l'elaborazione dei dati di postback.However, the added control does not catch up with postback data processing. Per un controllo aggiunto per partecipare all'elaborazione dei dati di postback, inclusa la convalida, è necessario aggiungere il controllo nell' Init evento anziché nell' Load evento.For an added control to participate in postback data processing, including validation, the control must be added in the Init event rather than in the Load event.
LoadControl(String)
public:
System::Web::UI::Control ^ LoadControl(System::String ^ virtualPath);
public System.Web.UI.Control LoadControl (string virtualPath);
member this.LoadControl : string -> System.Web.UI.Control
Public Function LoadControl (virtualPath As String) As Control
Parametri
- virtualPath
- String
Percorso virtuale di un file di controllo.The virtual path to a control file.
Restituisce
Restituisce l'oggetto Control specificato.Returns the specified Control.
Eccezioni
Il percorso virtuale è null
o vuoto.The virtual path is null
or empty.
Esempio
Nell'esempio di codice riportato di seguito viene illustrato come utilizzare il LoadControl metodo per aggiungere un controllo utente a una pagina ASP.NET.The following code example demonstrates how to use the LoadControl method to add a user control to an ASP.NET page.
Nel OnInit
metodo dell'evento, questa pagina usa il LoadControl metodo per creare a livello di codice un controllo utente nella pagina.In the OnInit
event method, this page uses the LoadControl method to programmatically create a user control on the page. Il metodo aggiunge il controllo alla Controls proprietà di un PlaceHolder controllo server Web denominato Placeholder1
.The method adds the control to the Controls property of a PlaceHolder Web server control named Placeholder1
.
Per la definizione della MyControl
classe utilizzata in questo esempio di codice, vedere TemplateControl .For the definition of the MyControl
class that is used in this code example, see TemplateControl.
void Page_Init(object sender, System.EventArgs e)
{
// Instantiate the UserControl object
MyControl myControl1 =
(MyControl)LoadControl("TempControl_Samples1.ascx.cs");
PlaceHolder1.Controls.Add(myControl1);
}
Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs)
' Obtain a UserControl object MyControl from the
' user control file TempControl_Samples1.ascx.vb
Dim myControl1 As MyControl = CType(LoadControl("TempControl_Samples1.vb.ascx"), MyControl)
Controls.Add(myControl1)
End Sub
Commenti
Se il controllo supporta la memorizzazione nella cache, l'oggetto restituito dal LoadControl metodo non è in realtà un Control oggetto, bensì un oggetto PartialCachingControl .If the control supports caching, the object returned from the LoadControl method is not actually a Control object; rather, it is a PartialCachingControl object. Se il virtualPath
parametro è relativo, il percorso del controllo caricato è relativo al percorso di TemplateControl .If the virtualPath
parameter is relative, the path of the loaded control is relative to the path of the TemplateControl.
Quando si carica un controllo in un controllo contenitore, il contenitore genera tutti gli eventi del controllo aggiunto fino a quando non viene rilevato l'evento corrente.When you load a control into a container control, the container raises all of the added control's events until it has caught up to the current event. Tuttavia, il controllo aggiunto non rileva l'elaborazione dei dati di postback.However, the added control does not catch up with postback data processing. Per un controllo aggiunto per partecipare all'elaborazione dei dati di postback, inclusa la convalida, è necessario aggiungere il controllo nell' Init evento anziché nell' Load evento.For an added control to participate in postback data processing, including validation, the control must be added in the Init event rather than in the Load event.
Vedi anche
- UserControl
- LoadControl(Type, Object[])
- OnInit(EventArgs)
- OnLoad(EventArgs)
- Panoramica del ciclo di vita della pagina ASP.NETASP.NET Page Life Cycle Overview
Si applica a
LoadControl(Type, Object[])
public:
System::Web::UI::Control ^ LoadControl(Type ^ t, cli::array <System::Object ^> ^ parameters);
public System.Web.UI.Control LoadControl (Type t, object[] parameters);
member this.LoadControl : Type * obj[] -> System.Web.UI.Control
Public Function LoadControl (t As Type, parameters As Object()) As Control
Parametri
- t
- Type
Tipo del controllo.The type of the control.
- parameters
- Object[]
Una matrice di argomenti che corrispondono per numero, ordine e tipo ai parametri del costruttore da richiamare.An array of arguments that match in number, order, and type the parameters of the constructor to invoke. Se parameters
è una matrice vuota oppure null
, viene chiamato il costruttore che non accetta parametri, ovvero il costruttore senza parametri.If parameters
is an empty array or null
, the constructor that takes no parameters (the parameterless constructor) is invoked.
Restituisce
Restituisce l'oggetto UserControl specificato.Returns the specified UserControl.
Commenti
Se il controllo utente supporta la memorizzazione nella cache, l'oggetto restituito dal LoadControl metodo non è in realtà un UserControl oggetto, bensì un oggetto PartialCachingControl .If the user control supports caching, the object returned from the LoadControl method is not actually a UserControl object; rather, it is a PartialCachingControl object.
Quando si carica un controllo in un controllo contenitore, il contenitore genera tutti gli eventi del controllo aggiunto fino a quando non viene rilevato l'evento corrente.When you load a control into a container control, the container raises all of the added control's events until it has caught up to the current event. Tuttavia, il controllo aggiunto non rileva l'elaborazione dei dati di postback.However, the added control does not catch up with postback data processing. Per un controllo aggiunto per partecipare all'elaborazione dei dati di postback, inclusa la convalida, è necessario aggiungere il controllo nell' Init evento anziché nell' Load evento.For an added control to participate in postback data processing, including validation, the control must be added in the Init event rather than in the Load event.
Vedi anche
- UserControl
- LoadControl(String)
- OnInit(EventArgs)
- OnLoad(EventArgs)
- Panoramica del ciclo di vita della pagina ASP.NETASP.NET Page Life Cycle Overview