TemplateControl.LoadControl 方法

定义

加载 ControlLoads a Control.

重载

LoadControl(String)

根据指定的虚拟路径从文件加载 Control 对象。Loads a Control object from a file based on a specified virtual path.

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)

根据指定的虚拟路径从文件加载 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

控件文件的虚拟路径。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. 方法将控件添加到名为 Placeholder1PlaceHolder Web 服务器控件的 Controls 属性。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. 如果 virtualPath 参数是相对的,则加载的控件的路径相对于 TemplateControl的路径。If 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[])

根据指定类型和构造函数参数加载 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

控件的类型。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.

另请参阅

适用于