c# – 代码隐藏页面无法“查看”aspx页面中声明的任何项目/控件
作者:互联网
这是我的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,net,asp-net,compiler-errors,code-behind 来源: https://codeday.me/bug/20190609/1208414.html