ASP.NET Syntax ASP.NET Page Syntax Page Directives
Declaratively indicates that another user control or page source file should be dynamically compiled and linked against the page in which this directive is declared.
<%@ Reference page | control = "pathtofile" %>
Page |
Web Form that ASP.NET should dynamically compile and link the current page against at runtime. |
Control |
User control that ASP.NET should dynamically compile and link the current page against at runtime. |
Using this directive allows you to dynamically compile a page or user control and add it to the ControlCollection object, accessed through the Controls property, for the page or server control. This allows you to cast the returned type after you have called the LoadControl method ( inherited by the page from the TemplateControl class ).
The below code snippet demonstrates using this directive to link a user control, MyControl.ascx, and load it to a containing page using the LoadControl method. When it is loaded to the page, the user control’s MyProperty value is set, and the user control is added to a PlaceHolder server control’s ControlCollection object through the Controls property.
<%@ Reference Control = "MyControl.ascx" %>
<script language="C#" runat=server>
void Page_Load ( Object sender, EventArgs e ) {
MyControl myControl = ( MyControl )
Page.LoadControl ( "MyControl.ascx" );
myControl.MyProperty = "Color";
PlaceHolder.Controls.Add ( myControl );
}
</script>
<html>
<body>
<asp:placeholder id="PlaceHolder" runat=server />
</body>
</html>
<%@ Reference Control = "MyControl.ascx" %>
<script language="VB" runat=server>
Sub Page_Load ( sender As Object, e As EventArgs )
Dim myControl As MyControl = _
CType ( Page.LoadControl ( "MyControl.ascx" ), MyControl )
myControl.MyProperty = "Color"
PlaceHolder.Controls.Add ( myControl )
End Sub
</script>
<html>
<body>
<asp:placeholder id="PlaceHolder" runat=server />
</body>
</html> |
|
C# |
VB |
Web Forms User Controls Introduction to Web Forms