TemplateControl.LoadControl TemplateControl.LoadControl TemplateControl.LoadControl TemplateControl.LoadControl Method

定義

載入 ControlLoads a Control.

多載

LoadControl(String) LoadControl(String) LoadControl(String) LoadControl(String)

根據指定的虛擬路徑,從檔案載入 Control 物件。Loads a Control object from a file based on a specified virtual path.

LoadControl(Type, Object[]) LoadControl(Type, Object[]) LoadControl(Type, Object[]) LoadControl(Type, Object[])

根據指定的型別和建構函式參數,載入 Control 物件。Loads a Control object based on a specified type and constructor parameters.

備註

當您將控制項載入容器控制項時, 容器會引發所有新增控制項的事件, 直到到達目前的事件為止。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. 不過, 新增的控制項不會趕上回傳資料處理。However, the added control does not catch up with postback data processing. 若要讓加入的控制項參與回傳資料處理, 包括驗證, 必須在Init事件中加入控制項, 而不是Load在事件中新增。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) LoadControl(String) LoadControl(String) LoadControl(String)

根據指定的虛擬路徑,從檔案載入 Control 物件。Loads a Control object from a file based on a specified virtual path.

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

參數

virtualPath
String String String String

控制項檔案的虛擬路徑。The virtual path to a control file.

傳回

傳回指定的 ControlReturns the specified Control.

例外狀況

虛擬路徑為 null 或空白。The virtual path is null or empty.

範例

下列程式碼範例示範如何使用LoadControl方法, 將使用者控制項加入至 ASP.NET 網頁。The following code example demonstrates how to use the LoadControl method to add a user control to an ASP.NET page.

在事件方法中, 此頁面會LoadControl使用方法, 以程式設計方式在頁面上建立使用者控制項。 OnInitIn the OnInit event method, this page uses the LoadControl method to programmatically create a user control on the page. 方法會將控制項加入至名ControlsPlaceHolder Placeholder1之 Web 服務器控制項的屬性。The method adds the control to the Controls property of a PlaceHolder Web server control named Placeholder1.

如需此程式碼MyControl範例中使用之類別的定義, 請參閱TemplateControlFor 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

備註

如果控制項支援快取, 則從LoadControl方法傳回的物件Control實際上並不是物件, 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. 如果參數是相對的, 則載入之控制項的路徑會相對於的路徑TemplateControlvirtualPathIf the virtualPath parameter is relative, the path of the loaded control is relative to the path of the TemplateControl.

當您將控制項載入容器控制項時, 容器會引發所有新增控制項的事件, 直到到達目前的事件為止。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. 不過, 新增的控制項不會趕上回傳資料處理。However, the added control does not catch up with postback data processing. 若要讓加入的控制項參與回傳資料處理, 包括驗證, 必須在Init事件中加入控制項, 而不是Load在事件中新增。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(Type, Object[]) LoadControl(Type, Object[]) LoadControl(Type, Object[]) LoadControl(Type, Object[])

根據指定的型別和建構函式參數,載入 Control 物件。Loads a Control object based on a specified type and constructor parameters.

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

參數

t
Type Type Type Type

控制項的類型。The type of the control.

parameters
Object[]

引數的陣列,此引數在數目、順序和類型上符合要叫用的建構函式的參數。An array of arguments that match in number, order, and type the parameters of the constructor to invoke. 如果 parameters 是空陣列或 null,則會叫用未採用參數的建構函式 (無參數建構函式)。If parameters is an empty array or null, the constructor that takes no parameters (the parameterless constructor) is invoked.

傳回

傳回指定的 UserControlReturns the specified UserControl.

備註

如果使用者控制項支援快取, 則從LoadControl方法傳回的物件UserControl實際上並不是物件, 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.

當您將控制項載入容器控制項時, 容器會引發所有新增控制項的事件, 直到到達目前的事件為止。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. 不過, 新增的控制項不會趕上回傳資料處理。However, the added control does not catch up with postback data processing. 若要讓加入的控制項參與回傳資料處理, 包括驗證, 必須在Init事件中加入控制項, 而不是Load在事件中新增。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.

另請參閱

適用於