如何加载通过我的页面(aspx)中的 dll 引用的用户控件(ascx) [英] How to load a usercontrol(ascx) that is referenced thru dll in my page(aspx)

查看:30
本文介绍了如何加载通过我的页面(aspx)中的 dll 引用的用户控件(ascx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中有用户控件,我在另一个项目中添加了它的引用,该项目将包含我的 aspx 页面.如何在我的页面中附加控件.我遇到过我们应该使用带有虚拟路径的 loadcontrol 方法.但我对虚拟路径一无所知.请帮我解决这个问题.

I have usercontrol in oneproject and i added the reference of it in another project which will have my aspx page. How to append the control in my page. I have come across we should use loadcontrol method with virtual path. But i am not getting anything about virtual path. Pls help me on this.

推荐答案

如果你想动态加载那么你应该使用 LoadControl

If you want to load dynamically then you should use LoadControl

var c = this.LoadControl("~/AddressControl.ascx");
this.myPanel.Controls.Add(c);

如果您不想动态加载它,那么您只需要将标记中的控件指定为其他用户控件:

If you do not want to load it dynamically, then you just need to specify the control in your markup as nay other user control:

<AddressControl runat="server" ID="myControl" />

您的情况的问题是您实际上需要对 Web 应用程序下或虚拟目录下的 ASCX 文件的引用.

The problem in your case is that you actually need a reference to the ASCX file under your web application or under a virtual directory.

您必须将 ASCX 文件复制到您的应用程序中,您可以通过配置构建后操作来轻松完成该步骤

You would have to copy your ASCX file to your application, you can do that step easy by configuring the post-build actions

看看下面的文章,它详细描述了如何完成这个过程:

Take a look at the following article which describes in detail how to accomplish this process:

http://webproject.scottgu.com/CSharp/UserControls/UserControls.aspx

这就像一种解决方法,因为用户控件不是为跨项目共享而设计的,适合您情况的最佳控件称为服务器控件,它们专为此目的而设计

This is like a workaround, because user controls are not designed to be shared across projects, the best control that would fit your situation are called server controls, they are designed for this purpose

这篇关于如何加载通过我的页面(aspx)中的 dll 引用的用户控件(ascx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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