代码隐藏页面无法“看到”在 aspx 页面中声明的任何项目/控件

发布于 2025-01-05 11:09:33 字数 2920 浏览 3 评论 0原文

这是我的 Default.aspx 页面(删除了不必要的详细信息):

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="login">
    <!-- a bunch of markup has been removed, so this html will appear wrong and not valid, but it actually is -->
    <table align="center" width="80%" cellpadding="0" cellspacing="0" class="loginBg">
        <tr>
            <td colspan="2"><img src="images/Login_top.jpg" /></td>
        </tr>
        <asp:Panel runat="server" ID="pnlLoginIn">
        <tr>
            <td style="padding-left:20px;">Username</td>
            <td style="padding-right:15px;" align="right"><asp:TextBox id="txtUsername" runat="server" /></td>
            <asp:RequiredFieldValidator runat="server" ID="rfvUserName" ErrorMessage="*" ControlToValidate="txtUsername" ValidationGroup="credentials" Display="Dynamic" />
        </tr>
        <tr>
            <td style="padding-left:20px;">Password</td>
            <td style="padding-right:15px;" align="right"><asp:TextBox ID="txtPassword" TextMode="Password" runat="server" /></td>
                <asp:RequiredFieldValidator runat="server" ID="rfvPassword" ErrorMessage="*" ControlToValidate="txtPassword" ValidationGroup="credentials" Display="Dynamic" />
        </tr>
        <!-- BUT THE PANEL IS HERE?! -->
        <asp:Panel ID="pnlInvalidCredentials" runat="server">
        <tr>
            <td colspan="2" align="center" style="color: Red;"><asp:Literal runat="server" ID="litInvalidCredentials" Text="Invalid Username or Password" />
            </td>
        </tr>
        </asp:Panel>
<!-- more code has been removed down here...I just left the block commented in where the pnlInvalidCredential lives -->
    </asp:Content>

这是隐藏代码(删除了不必要的详细信息):

namespace webapp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            (webapp.MasterPages.MasterPage)Page.Master).headerImage = "default";
            this.Master.headerImage = "default";

            if (!Page.IsPostBack)
            {
                this.pnlInvalidCredentials.Visible = false; // error is here
                this.BindPressRoomItem();
            }
        }
    }
}

此页面/代码隐藏是顶层,不是任何文件夹或任何内容。此外,该项目是一个 ASP.NET Web 应用程序,它借用了旧网站站点项目的代码。我没有“添加现有”任何文件,所有文件都是“添加新”以防止兼容性问题。尝试了这个,没有用。

在代码隐藏中,每次尝试操作 aspx 页面中声明的项目都会导致如下错误:

“_Default”不包含“pnlInvalidCredentials”的定义

名称“txtUsername”在此上下文中不存在。

Here's a my Default.aspx page (with unnecessary details excised):

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="login">
    <!-- a bunch of markup has been removed, so this html will appear wrong and not valid, but it actually is -->
    <table align="center" width="80%" cellpadding="0" cellspacing="0" class="loginBg">
        <tr>
            <td colspan="2"><img src="images/Login_top.jpg" /></td>
        </tr>
        <asp:Panel runat="server" ID="pnlLoginIn">
        <tr>
            <td style="padding-left:20px;">Username</td>
            <td style="padding-right:15px;" align="right"><asp:TextBox id="txtUsername" runat="server" /></td>
            <asp:RequiredFieldValidator runat="server" ID="rfvUserName" ErrorMessage="*" ControlToValidate="txtUsername" ValidationGroup="credentials" Display="Dynamic" />
        </tr>
        <tr>
            <td style="padding-left:20px;">Password</td>
            <td style="padding-right:15px;" align="right"><asp:TextBox ID="txtPassword" TextMode="Password" runat="server" /></td>
                <asp:RequiredFieldValidator runat="server" ID="rfvPassword" ErrorMessage="*" ControlToValidate="txtPassword" ValidationGroup="credentials" Display="Dynamic" />
        </tr>
        <!-- BUT THE PANEL IS HERE?! -->
        <asp:Panel ID="pnlInvalidCredentials" runat="server">
        <tr>
            <td colspan="2" align="center" style="color: Red;"><asp:Literal runat="server" ID="litInvalidCredentials" Text="Invalid Username or Password" />
            </td>
        </tr>
        </asp:Panel>
<!-- more code has been removed down here...I just left the block commented in where the pnlInvalidCredential lives -->
    </asp:Content>

Here's the code-behind (with unnecessary details excised):

namespace webapp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            (webapp.MasterPages.MasterPage)Page.Master).headerImage = "default";
            this.Master.headerImage = "default";

            if (!Page.IsPostBack)
            {
                this.pnlInvalidCredentials.Visible = false; // error is here
                this.BindPressRoomItem();
            }
        }
    }
}

This page/code-behind is top level, it's not it any folder or anything. Also this project is an ASP.NET Web Application that is borrowing code from a legacy web site project. I didn't "Add Existing" to any files, all files were "Add New" to prevent compatibility problems. Tried this, didn't work.

In the code-behind, every attempt to manipulate items declared in the aspx page results in an error like:

'_Default' does not contain a definition for 'pnlInvalidCredentials'

or

The name 'txtUsername' does not exist in this context.

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

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

发布评论

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

评论(4

抹茶夏天i‖ 2025-01-12 11:09:33

由于我刷新了此页面,您已删除了 html 文件顶部带有 page 指令的行,但看起来您可能需要包含名称空间。

尝试:

Inherits =“webapp._Default”而不是Inherits =“_Default”

Since I refreshed this page you've removed the line with the page directive at the top of your html file, but it looked like you maybe need to include the namespace.

Try:

Inherits="webapp._Default" instead of Inherits="_Default"

時窥 2025-01-12 11:09:33

尝试将 Default.aspx 页面中的“CodeBehind”属性更改为“CodeFile”。

Try changing the "CodeBehind" attribute in your Default.aspx page to "CodeFile."

萌化 2025-01-12 11:09:33

如果这是一个 Web 应用程序,有时会发生 *.designer.cs 文件未使用您的最新更改进行更新的情况。

试试这个:

  • 删除面板(只需剪切它)
  • 保存
  • 撤消
  • 再次保存(这将重新生成 *.designer.cs 文件)
  • 重建

编辑:

导致 *.designer.cs 文件重新生成失败的其他可能性是语法错误,例如您的 aspx 页面中缺少结束标记。

If this is a web application, sometimes it happens that the *.designer.cs file is not updated with your latest changes.

Try this:

  • Remove the panel (just cut it)
  • Save
  • Undo
  • Save again (this will regenerate the *.designer.cs file)
  • Rebuild

Edit:

Other possibilities that will fail the regeneration of the *.designer.cs file are syntax errors such as missing end tags in your aspx pages.

私野 2025-01-12 11:09:33

我刚刚遇到了同样的问题。为我解决的问题是:

  • 在我的代码隐藏文件中,该类被定义为“公共类”。将其更改为“部分类”,然后单击“转换为 Web 应用程序”重新生成“设计器”文件并解决了问题。

I just had the same problem. What fixed it for me was:

  • in my code behind file, the class was defined as 'Public class'. Changing this to 'Partial Class', then clicking 'Convert to Web Application' regenerated the 'Designer' file and fixed the problem.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文