从代码隐藏文件访问文本框,标签等

时间:2014-10-01 00:02:49

标签: c# asp.net webforms

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

2 个答案:

答案 0 :(得分:1)

您已在html中调用了密码框ID="txtBxPassword",但在后面的代码中却试图以txtBxPassword_Login

的形式访问它

答案 1 :(得分:0)

当您自动向页面添加新项目时,代码将被添加到另一个带有.designer.cs的文件中,例如,当您将Label放在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;

有时设计师不会添加此代码,因此未定义此项目后面的代码。