.NET Framework Class Library
TemplateControl..::.LoadControl Method (Type, array<Object>[]()[])

Loads a Control object based on a specified type and constructor parameters.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
Public Function LoadControl ( _
    t As Type, _
    parameters As Object() _
) As Control
Visual Basic (Usage)
Dim instance As TemplateControl
Dim t As Type
Dim parameters As Object()
Dim returnValue As Control

returnValue = instance.LoadControl(t, _
    parameters)
C#
public Control LoadControl(
    Type t,
    Object[] parameters
)
Visual C++
public:
Control^ LoadControl(
    Type^ t, 
    array<Object^>^ parameters
)
JScript
public function LoadControl(
    t : Type, 
    parameters : Object[]
) : Control

Parameters

t
Type: System..::.Type
The type of the control.
parameters
Type: array<System..::.Object>[]()[]
An array of arguments that match in number, order, and type the parameters of the constructor to invoke. If parameters is an empty array or nullNothingnullptra null reference (Nothing in Visual Basic), the constructor that takes no parameters (the default constructor) is invoked.

Return Value

Type: System.Web.UI..::.Control
Returns the specified UserControl.
Remarks

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. 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.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Community Content

Matthew Rowan
Does not instantiate controls defined on ascx page

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=103456

Description
A page that loads a user control using the Page.LoadControl(Type, Object[]) does not seem to create its children added in the ascx file.
Using Page.LoadControl(String) works as expected.


Comments
Thank you for submitting this issue. We're investigating and will provide an update on status when we have more information.

-The Web Platform & Tools Team
Posted by Microsoft on 8/06/2005 at 11:08 AM
This is by-design since the type "TestUC" is actually the base type used by the partial class, it does not contain the proper code to instantiate TextBox1 reference, which is actually defined in the derived type. There are two workarounds: 1. Use LoadControl("TestControl.ascx"), for all practical, this behaves identically to LoadControl(type) but it instantiates the derived type, which knows how to instantiate TextBox1. 2. Use a single file page and adds <%@ Reference %> directive to the page to reference the user control, and assign a classname to the ascx page. Then it's safe to use LoadControl(type)

Thanks for reporting the issue.
Web Platform and Tools Team.
Posted by Microsoft on 14/06/2005 at 6:31 PM

Tags : contentbug

Page view tracker