UserControl何时何地编译

时间:2009-04-06 09:32:11

标签: asp.net

我有一个页面Default.aspx和一个UserControl,HelloControl.ascx。在页面中,我按如下方式动态实例化控件:

protected void Page_Load(object sender, EventArgs e)
{
    HelloControl c = Page.LoadControl(typeof (HelloControl), null) as HelloControl;
    c.Greet();
}

这很好用,用户控件将“Hello from a control”写入响应。我在Default.aspx中没有@Register指令,但是当我在客户机的机器上尝试类似的动态控件创建时,我收到“类型或命名空间不存在”的错误。

我甚至在论坛上得到了来自MS的反馈,我需要@Register指令,但我显然没有。如果没有@Register指令引用用户控件的编辑方式和时间,有人可以帮我解决吗?

编辑:我尝试了不同的调查方向,失去了对LoadControl的初始调用,但仍然无法重现问题。以下代码也适用于我的开发机器,没有任何@Register指令。

protected void Page_Load(object sender, EventArgs e)
{
    HelloControl c = new HelloControl();
    Response.Write(c.Greet());
}

1 个答案:

答案 0 :(得分:0)

使用完整路径来控制:

this.Controls.Add(Page.LoadControl("~/CustomControls/HelloControl.ascx"));