c# - 代码隐藏页面不能 "see"在 aspx 页面中声明的任何项目/控件

标签 c# asp.net .net compiler-errors code-behind

这是我的 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 应用程序,它从遗留 Web 站点 项目中借用代码。我没有“添加现有”到任何文件,所有文件都是“添加新的”以防止兼容性问题。试过this ,没用。

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

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

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

最佳答案

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

尝试:

Inherits="webapp._Default"而不是 Inherits="_Default"

关于c# - 代码隐藏页面不能 "see"在 aspx 页面中声明的任何项目/控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9282403/

相关文章:

c# - 使用Couchbase时发生类型转换错误

c# - 如何对多个实例使用 Using 语句?

asp.net - 响应来自 ASP.NET 页面代码后面的 404 错误

.net - String.Format不格式化字符串

c# - 返回 MS.InternalMemoryStream 的简单网络客户端请求

c# - 如何检测文本的文化信息

c# - .NET Google Calendar API 获取事件的颜色

c# - List<T> 构造函数 (IEnumerable<T>) 不接受从 Array.CreateInstance(Type, Int32) 创建的数组

c# - 无法将项目更新到 .NET 5

css - MVC 中的 DropDownList 在 IE8 中不起作用