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.

OnInit事件的方法,此頁面會使用LoadControl以程式設計方式建立頁面上的使用者控制項的方法。In the OnInit event method, this page uses the LoadControl method to programmatically create a user control on the page. 此方法將控制項加入至Controls的屬性PlaceHolderWeb 伺服器控制項,名為Placeholder1The 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. 如果virtualPath參數是相對,載入的控制項的路徑是相對於路徑TemplateControlIf 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 default 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.

另請參閱

適用於