动态在asp.net网站中加载用户控件(ascx) [英] Dynamically Load a user control (ascx) in a asp.net website

查看:308
本文介绍了动态在asp.net网站中加载用户控件(ascx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在asp.web站点中动态加载用户控件。但是由于asp.net网站项目的设置(我想),我无法访问达到用户控件的类型定义。



我收到一条消息说我的类HE_ContentTop_WebControl1是:他的类型或命名空间名称'HE_ContentTop_WebControl1'找不到(你缺少一个using指令或程序集引用?)



任何想法如何这可以做到工作吗?我尝试使用命名空间,但在我看来,asp.net网站默认情况下不能使用命名空间。我会对非命名空间方法感兴趣。



TIA

 公共部分类HE_Default:
系统.Web.UI.Page {

protected void Page_Load(object sender,EventArgs e)
{
var control =(HE_ContentTop_WebControl1)Page.LoadControl(〜/ ContentTop / WebControl1的.ascx);
}
}


解决方案

假设控件存在于与您的Web项目相同的程序集中,您需要在.aspx文件中添加引用指令,



例如:

 <%@ Reference Control =〜/ Controls / WebControl1.ascx>请记住,智能感知通常需要几分钟(或有时是一个构建)来选择这个。 。


I am trying to dynamically load a user control in an asp.web site. However due to how asp.net websites projects are setup (I think), I am not able to access reach the type definition of the user control.

I get a message saying that my class HE_ContentTop_WebControl1 is: he type or namespace name 'HE_ContentTop_WebControl1' could not be found (are you missing a using directive or an assembly reference?)

Any idea how this could be made to work ? I have attempted using namespace but it seems to me that asp.net websites are not designed to work with namespaces by default. I would be interested in a non namespace approach.

TIA

public partial class HE_Default :
     System.Web.UI.Page   {  

    protected void Page_Load(object sender, EventArgs e)  
    {  
       var control = (HE_ContentTop_WebControl1)Page.LoadControl("~/ContentTop/WebControl1.ascx");         
    }
}

解决方案

Assuming the control exists in the same assembly as your web project, you need to add a reference directive in your .aspx file,

e.g:

<%@ Reference Control="~/Controls/WebControl1.ascx">

Keep in mind it often takes a few minutes (or sometimes a build) for IntelliSense to pick this up.

这篇关于动态在asp.net网站中加载用户控件(ascx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆