2014-10-01 174 views
0

我有一个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; 
    } 
} 
+0

我在控件运行时将控件添加到页面时会出现此问题。后面的代码不会自动生成。检查后面的设计器代码。控制属性在那里自动生成? – 2014-10-01 00:07:49

+0

在代码中,您使用txtBxPassword_Login.but在其用户名中使用txtBxPassword – Seminda 2014-10-01 00:12:15

+0

检查或交叉验证是否使用/映射Login.aspx.designer.cs文件与项目中的任何其他设计器页面。在整个项目中搜索Login.aspx.cs – Malcolm 2014-10-01 06:43:01

回答

1

你叫您的密码框ID="txtBxPassword"在html中,但在您的代码后面试图访问它作为txtBxPassword_Login

+0

是的,这是真的。 – 2014-10-01 00:08:41

+0

啊,这只是一个复制粘贴错误。固定。 – 2014-10-01 00:16:06

0

当您自动向页面添加新项目时,代码将被添加到另一个带有.designer.cs的文件中,例如,当您将标签放在default.aspx页面上时,您应该在默认页面上看到以下代码。 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; 

有时设计师不会添加此代码,因此在此代码背后的代码中未定义。

+0

设计器文件定义了所有标签,按钮等的代码。在OP中发布设计器代码以进行澄清。 – 2014-10-01 01:06:10

相关问题