简体   繁体   English

动态加载asp.net网站中的用户控件(ascx)

[英]Dynamically Load a user control (ascx) in a asp.net website

I am trying to dynamically load a user control in an asp.web site. 我试图在asp.web站点中动态加载用户控件。 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. 但是由于asp.net网站项目的设置(我认为),我无法访问达到用户控件的类型定义。

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?) 我收到一条消息,说我的类HE_ContentTop_WebControl1是:他找不到类型或命名空间名称'HE_ContentTop_WebControl1'(你是否缺少using指令或程序集引用?)

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. 我试图使用命名空间,但在我看来,asp.net网站默认情况下不适用于命名空间。 I would be interested in a non namespace approach. 我会对非命名空间方法感兴趣。

TIA 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, 假设控件存在于与Web项目相同的程序集中,则需要在.aspx文件中添加引用指令,

eg: 例如:

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

Keep in mind it often takes a few minutes (or sometimes a build) for IntelliSense to pick this up. 请记住,智能感知通常需要几分钟(或有时是构建版本)来进行选择。

It can easily be done using namespaces. 可以使用命名空间轻松完成。 Here's an example: 这是一个例子:

WebControl1.ascx: WebControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebControl1.ascx.cs" Inherits="MyUserControls.WebControl1" %>

Notice that Inherits references the namespace (MyUserControls), and not just the class name (WebControl1) 请注意, Inherits引用了命名空间(MyUserControls),而不仅仅是类名(WebControl1)

WebControl1.ascx.cs: WebControl1.ascx.cs:

namespace MyUserControls
{
    public partial class WebControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Notice that the class have been included in the namespace MyUserControls 请注意,该类已包含在命名空间MyUserControls中

Default.aspx.cs: Default.aspx.cs:

using MyUserControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var control = (WebControl1) Page.LoadControl("~/WebControl1.ascx");
    }
}

This approach potentially allow you to redistribute your user controls (or keep them in a separate project) without the hassle of referencing them in your .aspx files. 这种方法可能允许您重新分发用户控件(或将它们保存在单独的项目中),而无需在.aspx文件中引用它们。

Namespaces are not supported under the website model. 网站模型不支持命名空间。 As such, I could not get any of the solutions proposed to work. 因此,我无法获得任何提议的解决方案。 However, there is a solution. 但是,有一个解决方案。 Create an interface and place it into app code and then implement the interface in the user control. 创建一个接口并将其放入应用程序代码中,然后在用户控件中实现该接口。 You can cast to the interface and it works. 你可以转换到界面,它的工作原理。

The subject of this post is a bit misleading. 这篇文章的主题有点误导。 If you just want to add a control dynamically, you will not have to reference the control and therefore you can just add it with something simple like: 如果您只想动态添加控件,则不必引用该控件,因此您只需添加简单的内容即可:

protected void Page_Load(object sender, EventArgs e)
{
    Page.Controls.Add(Page.LoadControl("~/MyControl.ascx")); 
}

without any namespace hassel. 没有任何命名空间。

the reference is not enough using 使用参考是不够的

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

in the aspx file is just one part of the answer. 在aspx文件中只是答案的一部分。

you need also to add the calssName in the User Control aspx file 您还需要在User Control aspx文件中添加calssName

<%@ Control ClassName="WebControl1" Language="C#" AutoEventWireup="true" CodeFile="WebControl1.ascx.cs" Inherits="AnySpaceName.DateSelector" %>

and then you can use the userontrol in your aspx file 然后你可以在你的aspx文件中使用userontrol

AnySpaceName.WebControl1 WC = (AnySpaceName.WebControl1)Page.LoadControl("~/WebControl1.ascx");    

Casting the user control this way may create many problems .my approach is to create a class (say control Class) put all the properties and method you need for casting in it and inherit this class from System.Web.UI.UserControl .Then in your user cotrol code file instead of System.Web.UI.UserControl user this control class . 以这种方式强制转换用户控件可能会产生许多问题。我的方法是创建一个类(比如控件类),将所需的所有属性和方法放入其中并从System.Web.UI.UserControl继承此类。然后进入您的用户cotrol代码文件而不是System.Web.UI.UserControl用户此控件类。

now when ever you need casting, cast with this class only . 现在什么时候你需要施法,只用这个类施放。 it will be light casting as well. 它也将是轻型铸造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM