动态创建(通过code)加载ASCX控制 [英] Loading ASCX control created dynamically (via code)

查看:163
本文介绍了动态创建(通过code)加载ASCX控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建通过code( Web用户控件我的C#code写出来的标记,code-后面,设计师的文件到硬盘在回调)。该控件创建的所有罚款。我可以将它们添加到我的网络项目,并把它们放在一个页面上。

I am creating Web User Controls via code (my C# code write-out markup, code-behind and designer files to disk on a callback). The controls are created all fine. I can add them to my web-project and put them on a page.

当我尝试使用加载控制 LoadControl(路径)它说:

When I try to load the control using LoadControl(path) it says:

Unable to load type 'MyNameSpace.UseControlClass'

这是因为控制尚未编译。

which is because control is not yet compiled.

但我的要求是动态加载控制而无需重新编译解决方案。

But my requirement is to load controls dynamically without recompiling the solution.

我怎么能只编译用户控件,当我创建控制文件?由于这似乎是唯一的出路。

How can I compile the user control only when I create the control files? As this seems to be the only way out.

修改: - 我的猜测是什么,由于文件尚未编译它不允许通过运行时加载。我试图编译使用 codeDOM 编译code文件。这样的:

EDIT:- What my guess is that as the file is not compiled yet it is not allowed to be loaded by runtime. I have tried to compile the code file using CodeDom compiler. Like:

var filePath = Server.MapPath(path);
var provider = CSharpCodeProvider.CreateProvider("C#");
var opts = new CompilerParameters(new[] { "mscorlib.dll", "System.Web.dll", 
                              "Telerik.Web.Design.dll", "Telerik.Web.UI.dll", 
                              "Telerik.Web.UI.Skins.dll", "MyCurrentDll.dll"});
opts.GenerateExecutable = false;
opts.GenerateInMemory = true;
var cr = provider.CompileAssemblyFromFile(opts, new string[] { filePath+".cs" });

但它抱怨无法找到元数据文件Telerik.Web.Design.dll 等。我不想硬code,因为它可能会在Telerik的路径在托管系统不同(虽然它是当前Web应用程序的)。此外 MyCurrentDll.dll 是我从中编译code文件的文件的DLL。我怎样才能解决这个问题?

But it complains about cannot find metadata file Telerik.Web.Design.dll etc. I don't want to hardcode the telerik path as it might be different in hosted system (though it is in the bin of current web app). Also MyCurrentDll.dll is the dll of the file from which I'm compiling the code file. How can I resolve this?

我的想法是编译code文件中创建一个 DLL 动态并将其复制到Web应用程序的目录。它可以解决我原先规定的问题。

My idea is to compile the code file create a dll dynamically and copy it to web application's bin directory. It might solve the problem I originally stated.

编辑2 : - 命中和审判之后,我能够动态编译code文件并生成 DLL 。即使生成的dll并将其置于斌我的应用程序的后,我不能够使用虚拟路径加载用户的控制。我曾尝试以下方法:

EDIT 2:- After hit and trial I am able to compile the code file dynamically and generate the dll. Even after generating dll and placing it into bin of my application I am not able to load user control using virtual path. I have tried the following approach:

var asm = Assembly.Load("ddlPath");
var t = asm.GetType(fullTypeName);//NameSpace.Class
var ctrl = LoadControl(t,null);

该CTRL由此而加载。我分配它编号属性,并将其添加到 asp.net面板控制。但它是不可见的回传后:(

The ctrl is loaded after this. I assign its Id property and add it to an asp.net Panel control. But it is not visible after the postback :(

现在我要么必须作出某种方式提供给运行时动态编译的DLL类型(AppDomain中,也许),所以,当我使用虚拟路径是正确加载,我没有得到加载控制 HtmlParseException 或弄清楚为什么内参形式键入没有显示出来。

Now I either have to make somehow the dynamically compiled dll's types available to the runtime (appdomain, maybe) so that when I load control using virtual path it is properly loaded and I don't get HtmlParseException or figure out why loading control form Type is not showing up.

PS: - 我已加载了标签控制使用键入键,可以正常工作

PS:- I have loaded a Label control using Type and it is working properly.

推荐答案

在最后我能解决这个问题。以下是我采取的策略:

In the end I was able to resolve the problem. Following is the strategy I adopted:


  1. 创建包含 codeBehind 标记设计师<结构文本文件/ code> code

  2. 阅读信息,以什么字段来创建。

  3. 把标记为字符串建设者需要控制。

  4. 把其他一些相关信息转换成多个字符串建设者

  5. 写入文件

这之后,我创建了使用 codeDome 编译 DLL 。编译过程中所面临的主要问题,如 修改 问题的一部分,并没有发现引用的程序,它是通过将<$路径解析C $ C>斌目录的像dll文件名沿路径:

After this I created the dll using CodeDome compiler. The main problem faced during compilation, as described in Edit part of question, was not finding referenced assemblies, which was resolved by putting path of bin directory's path along with dll file name like:

Server.MapPath("~/bin")+"\\Telerik.Web.dll"

等。

中列出接下来的问题 EDIT2 是有点简单。有相当鲁钝。有嵌入在动态生成的用户控制其他用户控件的一些问题。

Next problem outlined in EDIT2 was bit simpler. There was rather moronic. There was some problem in another user control embedded in dynamically generated user control.

在这之后我无法控制从我编译code加载到页面。这时候我加载其他页面控制得到解决。运行时能够解决从类型动态编译 DLL

Even after this i was not able to load the control into the page from where I compiled the code. This was resolved when I loaded the control in another page. The runtime was able to resolve the type from dynamically compiled dll.

这篇关于动态创建(通过code)加载ASCX控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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