【问题标题】:How can I access a UserControl from inside a class in App_code?如何从 App_code 中的类内部访问 UserControl?
【发布时间】:2014-02-06 09:05:35
【问题描述】:

在 ASP.net 网站项目中,我在 App_code 文件夹中有一个名为“abc”的类,并在根 MyControl/Menu.ascx 的文件夹中拥有用户控件。

现在我的问题是如何在“abc”类中创建我的 UserControl 实例?因为 UserControl 不可访问。

【问题讨论】:

    标签: c# app-code


    【解决方案1】:

    就这样试试

    CustomControl cc = (CustomControl)Page.LoadControl("...");
    

    【讨论】:

    • 这是不可能的,您必须首先使“CustomControl”类可访问。感谢您的评论
    • 是的,你有公共说明符吗?
    • 是的,我愿意。请注意,它是“ASP.net 网站”类型的项目,而不是“ASP.Net 项目”类型的项目。
    【解决方案2】:
    1. 在您的用户控件中包含正确的namespace

      using YourNamespace;

    2. 然后创建uer控件的实例

      UserControlClass userconrtolinstance = new UserControlClass();

    【讨论】:

    • 我也试过这个,请在​​下面查看。命名空间 MyTheme { public class abc() { UserControlClass userconrtolinstance = new UserControlClass();我也为控件分配了相同的命名空间。它仍然没有找到它。
    • 你在using block中使用的命名空间是什么?
    • 我创建了一个名为“MyTheme”的命名空间,控件和类在同一个命名空间下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2017-02-14
    • 2011-01-02
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多