[英]Programmatically load User Control in code-behind registered in web.config
Is it possible to load a User Control that was registered in the web.config file, rather than in the aspx file?是否可以加载在 web.config 文件而不是 aspx 文件中注册的用户控件?
Here is an example of what I would do.这是我会做的一个例子。 In my aspx:
在我的 aspx 中:
index.aspx索引.aspx
<%@ Register TagPrefix="module" TagName="ModExample" Src="~/controls/example.ascx" %>
index.cs索引.cs
protected void Page_Load(object sender, EventArgs e)
{
controls_example exmp = LoadContent("controls/example.ascx") as controls_example;
myContentPanel.Controls.Add(exmp);
}
I had to register my user control in the page, or else ASP would not know what "controls_example" was.我必须在页面中注册我的用户控件,否则 ASP 将不知道“controls_example”是什么。 However, I know you can change your web.config to look like this:
但是,我知道您可以将 web.config 更改为如下所示:
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="module" tagName="ModExample" src="~/controls/contentModule.ascx"/>
<add assembly="Subtext.Web.Controls" namespace="Subtext.Web.Controls" tagPrefix="module"/>
</controls>
</pages>
</system.web>
</configuration>
So, here is the problem.所以,这就是问题所在。 How do I create a variable of type "controls_example" when it has been registered in the web.config file?
在 web.config 文件中注册后,如何创建“controls_example”类型的变量?
Do I need to:我需要:
Your web.config
looks fine.您的
web.config
看起来不错。 In the page you need to add a using
with the namespace of your control.在页面中,您需要添加一个
using
with 控件的命名空间。 Then you can use the page's LoadControl
method to get an instance of your control that you can then add as a child control to some container (I usually choose a asp:Placeholder
to ensure its position is where I want it)然后您可以使用页面的
LoadControl
方法获取您的控件的实例,然后您可以将其作为子控件添加到某个容器(我通常选择asp:Placeholder
以确保其 position 在我想要的位置)
@Adrian, here is my response to your question. @Adrian,这是我对你问题的回答。 I have to say it here, because I will run far over my char limit.
我必须在这里说出来,因为我会远远超出我的字符限制。
Ok, the first problem I had was that I was using a ASP.NET Website , not a ASP.NET Application .好的,我遇到的第一个问题是我使用的是 ASP.NET网站,而不是 ASP.NET应用程序。 I chaged this so I was using the latter.
我改变了这个所以我使用了后者。 This made it so that all pages created from then on had a namespace by default (being the name of the folder they were in).
这使得从那时起创建的所有页面默认都有一个命名空间(即它们所在文件夹的名称)。 I do not know if you can register controls using a namespace in a website, maybe because websites don't have namespaces by default.
我不知道您是否可以在网站中使用命名空间注册控件,可能是因为默认情况下网站没有命名空间。
So let's say I made a Web User Control called myControl.ascx
, and saved it as ~/controls/myControl.ascx
.因此,假设我创建了一个名为 myControl.ascx 的
myControl.ascx
用户控件,并将其保存为~/controls/myControl.ascx
。 Looking at the ascx file, the top line should read: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myControl.ascx.cs" Inherits="myApplication.controls.myControl" %>
.查看 ascx 文件,顶行应显示为:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myControl.ascx.cs" Inherits="myApplication.controls.myControl" %>
。 myApplication
is the name of your website, and you can seed it added controls
to the namespace because that is the folder it is in. Also, if you look at the code behind, the namespace will match. myApplication
是您网站的名称,您可以为它添加controls
到命名空间,因为这是它所在的文件夹。此外,如果您查看后面的代码,命名空间将匹配。
Now, from here you have 2 options on how to register and use the control.现在,从这里您有 2 个关于如何注册和使用控件的选项。 The slow way is to register each control you want to use on each page you have, which is good if you only have a few of each.
较慢的方法是在您拥有的每个页面上注册您要使用的每个控件,如果每个控件只有几个,这很好。 To do this, write the following line in the page you want to call the control from (like the index page, for example):
为此,请在要从中调用控件的页面(例如索引页面)中写入以下行:
<%@ Register TagPrefix="myC" TagName="myCoolControl" Src="~/control/myControl.ascx" %>
That is the slow way, but it works (unless I typed something wrong).这是缓慢的方式,但它有效(除非我输入错误)。
Here is what I was trying to do: make it so I don't have to type this a thousand times, and only do it once in the web.config
file.这是我试图做的事情:做到这一点,这样我就不必输入一千次,并且只在
web.config
文件中输入一次。
Configure your web.config
file to look like this:将您的
web.config
文件配置为如下所示:
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="myCont" namespace="myApplication.controls" assembly="myApplication" />
</controls>
</pages>
</system.web>
</configuration>
This will automaticly register any control made in the "myApplication.controls" namespace, which, if you set things up correctly, is any control made in the "controls" folder.这将自动注册在“myApplication.controls”命名空间中制作的任何控件,如果您设置正确,它就是在“controls”文件夹中制作的任何控件。 Just remember, you don't have to call the folder "controls" for this to work, you may have any namespace listed here and it should work just fine.
请记住,您不必调用文件夹“controls”来使其工作,您可以在此处列出任何命名空间,它应该可以正常工作。
Now open up a page you want to load a control from programmatic (like index).现在打开一个页面,你想从编程(如索引)加载一个控件。 You are NOT going to write anything in the aspx file, including anything about registering the page.
您不会在 aspx 文件中写入任何内容,包括有关注册页面的任何内容。 The control is already registered, so all we have to do is load it in the code-behind:
该控件已经注册,所以我们要做的就是在代码隐藏中加载它:
myControl myCont = LoadControl("~/controls/myControl.ascx") as myControl;
//feel free to use the variable, or add it to the page with a panel or something
I really hope this answered your question.我真的希望这能回答你的问题。 It did take some digging around on my end to get it to work, so I'm glad to help others out with this problem.
我确实花了一些时间来让它工作,所以我很高兴帮助其他人解决这个问题。 Thanks again to CyberDude, who got me going in the right direction.
再次感谢 CyberDude,是他让我朝着正确的方向前进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.