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.
我试图在asp.web站点中动态加载用户控件。但是由于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. I would be interested in a non namespace approach.
知道如何才能使它工作吗?我曾尝试使用命名空间,但在我看来,asp.net网站默认情况下不适用于命名空间。我会对非命名空间方法感兴趣。
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");
}
}
6 个解决方案
#1
10
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文件中添加引用指令,
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.
请记住,智能感知通常需要花费几分钟(或有时是构建版本)来进行选择。
#2
7
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文件中引用它们。
#3
4
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.
网站模型不支持命名空间。因此,我无法获得任何提议的解决方案。但是,有一个解决方案。创建一个界面并将其放入应用程序代码中,然后在用户控件中实现该界面。你可以转换到界面,它的工作原理。
#4
2
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");
#5
2
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.
没有任何命名空间。
#6
1
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.
现在什么时候你需要施法,只用这个类施放。它也将是轻型铸造。
#1
10
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文件中添加引用指令,
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.
请记住,智能感知通常需要花费几分钟(或有时是构建版本)来进行选择。
#2
7
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文件中引用它们。
#3
4
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.
网站模型不支持命名空间。因此,我无法获得任何提议的解决方案。但是,有一个解决方案。创建一个界面并将其放入应用程序代码中,然后在用户控件中实现该界面。你可以转换到界面,它的工作原理。
#4
2
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");
#5
2
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.
没有任何命名空间。
#6
1
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.
现在什么时候你需要施法,只用这个类施放。它也将是轻型铸造。