无法从 aspx 页面引用 UserControl

发布于 2024-11-08 21:49:23 字数 1188 浏览 0 评论 0原文

我已将一个名为 HomePageItem 的 Web 用户控件添加到我的项目中,并且我试图以编程方式从名为 LandingPage.aspx 的页面中创建它的新实例。由于某种原因,该类不可见。我刚刚收到一个生成错误,显示:

找不到类型或命名空间名称“Controls_HomePageItem”(您是否缺少 using 指令或程序集引用?)

这是我的 LandingPage.aspx 文件中的代码行:

Controls_HomePageItem hpi = new Controls_HomePageItem();

这里是整个 HomePageItem.ascx 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_HomePageItem : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

我尝试将控件放入名为 HomePage
的命名空间中 像这样:

namespace HomePage {
    public partial class Controls_HomePageItem : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {  

        }
    }
}

然后像 LandingPage.aspx 中那样引用 Control HomePage.Controls_HomePageItem hpi = new HomePage.Controls_HomePageItem();

我尝试了顶部的 using 语句:

using HomePage;

仍然存在同样的问题。我什至看不到 LandingPage.aspx 中的 HomePage 命名空间

请帮忙。
TIA

I have added a Web User Control called HomePageItem to my project and I am trying to create a new instance of it in from a page called LandingPage.aspx programatically. For some reason the class is not visible. I just get an build error that says:

The type or namespace name 'Controls_HomePageItem' could not be found (are you missing a using directive or an assembly reference?)

This is the line of code in my LandingPage.aspx file:

Controls_HomePageItem hpi = new Controls_HomePageItem();

And here is the entire HomePageItem.ascx file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_HomePageItem : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

I tried putting the control in a namespace called HomePage
like so:

namespace HomePage {
    public partial class Controls_HomePageItem : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {  

        }
    }
}

And then referencing the Control like so in LandingPage.aspx
HomePage.Controls_HomePageItem hpi = new HomePage.Controls_HomePageItem();

and I tried a using statement at the top:

using HomePage;

Same issues still. I cant even see the HomePage namespace from LandingPage.aspx

Please help.
TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情废物 2024-11-15 21:49:23

我也有这个问题。我解决了。当我将用户控件的 Register 添加到 aspx 文件时,我可以在代码中引用用户控件类。我仅从代码创建用户控件,但需要注册。

PS:对不起我的英语。

I had this problem too. I solved it. When I add Register of my usercontrol to aspx file then I can reference usercontrol class in code. I create my usercontrol only from code, but Register is needed.

PS: Sorry for my English.

吲‖鸣 2024-11-15 21:49:23

你那里没有命名空间行吗?

在 using 和 public 分部类之间,应该有一个命名空间线。

You dont have a namespace line in there?

between the using and the public partial class, there should be a namespace line.

红衣飘飘貌似仙 2024-11-15 21:49:23

好的,找到问题了。
该项目是作为网站而不是 Web 应用程序创建的。 Visual Studio 不断在后台构建新控件,但我的构建存在问题,因为某些引用的 DLL 是由其他人从源代码管理中专门检出的。该网站仍将构建和运行等,但不会生成任何控件类。一旦我解决了签出的 DLL 的源代码控制问题,我的控件就开始正常构建。

谢谢大家的回复。

Ok, found the issue.
The project was created as a website and not a Web Application. Visual studio continually builds new controls in the background, but there was an issue with my build because some of the referenced DLLs were checked out exclusively out of source control by someone else. The website would still build and run and such, but none of the control classes were being generated. As soon as I sorted out the source control issue with the checked out DLLs, my controls started building normally.

Thank you for everyones responses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文