从代码隐藏 ascx.cs 文件访问 ascx 控件

发布于 2024-09-18 21:00:39 字数 729 浏览 5 评论 0原文

这个应该很简单。我正在制作一个非 MVC ASP.NET 2.0 网站。 VS2008 似乎生成带有

我有以下内容:

widget.ascx:

<%@ Control Language="C#" ClassName="widget" Codebehind="widget.ascx.cs" Inherits="widget"%>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

widget.ascx.cs:

namespace webapp
{
  public partial class widget : System.Web.UI.Control
  {
    protected void Page_Load(object sender)
    {
        Label1.Text = Session["user_id"].ToString();
    }
  }
}

我从使用代码隐藏文件的 ASPX 页面复制并粘贴了这些内容,但是当我尝试编译时,出现错误 Label1 在此上下文中不存在< /代码>。

感谢有关此事的所有帮助。

This one should be pretty simple. I am making a non-MVC ASP.NET 2.0 site. VS2008 seems to generate controls with a <script> area -- I want the code in a codebehind, so I've manually hooked that up.

I have the following:

widget.ascx:

<%@ Control Language="C#" ClassName="widget" Codebehind="widget.ascx.cs" Inherits="widget"%>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

widget.ascx.cs:

namespace webapp
{
  public partial class widget : System.Web.UI.Control
  {
    protected void Page_Load(object sender)
    {
        Label1.Text = Session["user_id"].ToString();
    }
  }
}

I copy and pasted this stuff from ASPX pages that use codebehind files, but when I try to compile I get errors that Label1 does not exist in this context.

All help in the matter appreciated.

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

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

发布评论

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

评论(3

花辞树 2024-09-25 21:00:39

这与您其他页面/控件的声明相符吗?

尝试使用 codebeside 而不是 codebehind / 更好地查看其他项目文件中的声明。

does that match the declaration of your other pages / controls?

try using codebeside instead of codebehind / better to look it at declarations in other project files.

隐诗 2024-09-25 21:00:39

好吧,我一定是以错误的方式创建了控件。我从一个新的模板开始,一切都按预期工作。如果您遇到此问题,只需复制代码并尝试要求 Visual Studio 再次从用户控件模板生成文件。确保右键单击项目名称并添加新文件。

Well, I must have created the control the wrong way. I started over with a fresh template and it all worked as expected. If you are experiencing this problem, just copy your code out and try to ask Visual Studio to generate the files from the user control templates again. Make sure you right-click on project name and add a new file.

盛夏已如深秋| 2024-09-25 21:00:39

要了解这里发生的情况,请务必注意标记文件 (.ascx) 继承自代码隐藏文件 (.ascx.cs),并且可以访问其非私有字段。反之亦然:您不能从代码隐藏中引用在标记文件中定义的对象。在您的示例中,这也可以通过将以下内容放入 widget.ascx.cs 文件中来解决:

protected Label Label1;

但是,什么!当您让 Visual Studio 重新执行控件时,您可能在 .ascx.cs 文件中看不到任何此类行。 Visual Studio 管理和维护第二个代码隐藏文件,即 .ascx.designer.cs 文件。 公共分部类小部件中的partial表示允许在多个文件中定义控件的代码。您管理 .ascx.cs 文件,Visual Studio 管理 .ascx.designer.cs。当您从标记文件中添加、删除和重命名控件时,Visual Studio 应该在设计器代码隐藏文件中添加、删除和重命名关联的基类字段。如果您删除、编辑或排除了此文件,则 Label1 可能无法访问。

To understand what's going on here, it's important to note that the markup file (.ascx) inherits from, and has access to the non-private fields of, the code-behind file (.ascx.cs). Not the other way around: you cannot reference objects defined only in the markup file from your code-behind. In your example, this would have also been solved by placing the following in your widget.ascx.cs file:

protected Label Label1;

But, what! When you had Visual Studio re-do the control, you probably don't see any such line in your .ascx.cs file. Visual Studio manages and maintains a second code-behind file, the .ascx.designer.cs file. The partial in public partial class widget signals that the code for the control is allowed to be defined in multiple files. You manage the .ascx.cs file, and Visual Studio manages the .ascx.designer.cs. As you add, remove, and rename controls from the markup file, Visual Studio should be adding, removing, and renaming the associated base-class fields in the designer code-behind file. If you deleted, edited, or excluded this file, then it's possible that Label1 could have been inaccessible.

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