2014-10-01 83 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

相關問題