我有一个Web窗体页,其中包含一个标签和两个文本框,我希望在该页的代码隐藏文件中访问它们,但是当我调用代码隐藏文件中的文本框时,标签和文本框调用的下方会显示红色的弯曲线。我已经尝试了类似问题中提到的解决方案(重新生成设计器文件,确保在其他页面中没有多个同名的文本框/标签,创建一个新的aspx页面并重写代码),但它们都没有解决这个作用域问题。这段代码昨晚编译得很好,从那以后我就没有修改过代码,它也没有编译。
我对C#比较陌生,所以我可能遗漏了一些非常明显的东西,为此我提前道歉。
在Login.aspx.cs中:
protected void btnCreateUser_Click(object sender, EventArgs e)
//this is called in an asp:button's OnClick
{
string loginUsername = txtBxUsername_Login.Text;//red line on txtBxUsername_Login
string loginPassword = txtBxPassword_Login.Text;//red line on txtBxPasswprd_Login
if (loginUsername != null && loginUsername != "" & loginPassword != null && loginPassword != "")
{
WebSecurity.Login(loginUsername, loginPassword, false);
lblLoginResponse.Text = "TO DO: Proper login response";//red line on lblLoginResponse
}
}在Login.aspx中:
<asp:Content ID="BodyContent" ContentPlaceHolderID="PageContent" runat="server">
<div class="row">
<div class="content-large">
<%-- This section tag contains all account validation controls, buttons, textboxes and labels --%>
<section id="loginForm">
<div class="form-horizontal">
<h2><%: Title %></h2>
<h4>Use a local account to login: </h4>
<hr />
<asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
<p class="text-danger">
<asp:Literal runat="server" ID="failureText" />
</p>
</asp:PlaceHolder>
<div class="form-group">
<div class="text-box-container">
<asp:Label ID="lblUsername" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="txtBxUsername_Login" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<div class="text-box-container">
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtBxPassword_Login" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<asp:Button ID="btnRegister" runat="server" Text="Log in" OnClick="btnLogin_Click" />
<asp:Label runat="server" ID="lblLoginResponse" />
</div>
</div>
</section>
</div>
</div>
</asp:Content>Login.aspx.designer.cs文件:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace inft3970.Account {
public partial class Login {
/// <summary>
/// ErrorMessage control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.PlaceHolder ErrorMessage;
/// <summary>
/// failureText control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Literal failureText;
/// <summary>
/// lblUsername control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblUsername;
/// <summary>
/// txtBxUsername_Login control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox txtBxUsername_Login;
/// <summary>
/// lblPassword control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblPassword;
/// <summary>
/// txtBxPassword control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox txtBxPassword_Login;
/// <summary>
/// btnRegister control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnRegister;
/// <summary>
/// lblLoginResponse control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblLoginResponse;
/// <summary>
/// btnCreateUser control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button btnCreateUser;
}
}发布于 2014-10-01 08:08:03
您在html中将密码框命名为ID="txtBxPassword",但在您的代码中,您试图以txtBxPassword_Login的身份访问它
发布于 2014-10-01 08:32:57
当您自动向页面添加新项时,代码将被添加到使用.designer.cs的另一个文件中。例如,当您将标签放在default.aspx页面上时,您应该会在default.aspx.designer.cs上看到以下代码:
/// <summary>
/// Label1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label1;有时设计器不添加此代码,因此在此项背后的代码中未定义此项。
https://stackoverflow.com/questions/26132167
复制相似问题