指示应该根据在其中声明此指令的当前 ASP.NET 文件(网页、用户控件或母版页),对位于某个虚拟路径的另一个用户控件、页源文件或任意文件进行动态编译和链接。
<%@ Reference Page="path to .aspx page" Control="path to .ascx file" virtualPath="path to file" %> |
属性
- Page
-
外部页,ASP.NET 应动态编译该页并将它链接到包含 @?Reference 指令的当前文件。
- Control
-
外部用户控件,ASP.NET 应动态编译该控件并将它链接到包含 @?Reference 指令的当前文件。
- virtualPath
-
引用的虚拟路径。只要生成提供程序存在,可以是任何文件类型。例如,它可能会指向母版页。
备注
使用此指令可以动态编译与生成提供程序关联的页面、用户控件或另一个类型的文件,并将其链接到包含 @?Reference 指令的当前网页、用户控件或母版页文件。这样您就可以从当前文件内部引用外部编译的对象及其公共成员。
示例
下面的代码示例演示如何使用该指令链接用户控件,以及如何使用 LabelText 值,并通过
| C#? | 复制代码 |
|---|---|
<%@ Control language="C#" ClassName="MyControl" %>
<script runat="server">
private string _labelText;
public string LabelText
{
get { return _labelText; }
set
{
if(!String.IsNullOrEmpty(value))
_labelText = Server.HtmlEncode(value);
}
}
void label1_init(object sender, EventArgs e)
{
label1.Text = LabelText;
}
</script>
<asp:label id="label1" runat="server" Text=""
oninit="label1_init" />
<%@ Page language="C#" %>
<%@ Reference Control="MyControl.ascx" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
MyControl ctrl = (MyControl) Page.LoadControl("MyControl.ascx");
ctrl.LabelText = "Hello World!";
PlaceHolder.Controls.Add(ctrl);
}
</script>
<html>
<body>
<asp:placeholder id="PlaceHolder" runat="server" />
</body>
</html> | |
| Visual Basic? | 复制代码 |
|---|---|
<%@ Control language="VB" ClassName="MyControl" %>
<script runat="server">
Dim _labelText As String
Public Property LabelText() as String
Get
Return _labelText
End Get
Set(Byval value as String)
If Not String.IsNullOrEmpty(value) Then
_labelText = Server.HtmlEncode(value)
End If
End Set
End Property
Sub label1_init(Byval sender as Object, _
ByVal e as EventArgs)
label1.Text = LabelText
End Sub
</script>
<asp:label id="label1" runat="server" Text=""
oninit="label1_init" />
<%@ Page language="VB" %>
<%@ Reference Control="MyControl.ascx" %>
<script runat="server">
Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs)
Dim ctrl As MyControl = _
CType(Page.LoadControl("MyControl.ascx"), MyControl)
ctrl.LabelText = "Hello World!"
PlaceHolder.Controls.Add(ctrl)
End Sub
</script>
<html>
<body>
<asp:placeholder id="PlaceHolder"
runat="server" />
</body>
</html> | |
请参见