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

本文关键字:标签 文本 代码 隐藏文件 访问 | 更新日期: 2023-09-27 18:25:13

我有一个Web Forms页面,它包含一个标签和两个文本框,我想在页面的代码隐藏文件中访问这些标签和文本框,但当我调用代码隐藏文件的文本框时,红色的歪歪扭扭的线显示在标签下面,文本框调用。我尝试过类似问题中提到的解决方案(重新生成设计器文件,确保其他页面中没有多个同名的文本框/标签,创建一个新的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;
    }
}

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

您在html中调用了密码框ID="txtBxPassword",但在代码后面却试图将其作为txtBxPassword_Login 访问

当您自动向页面添加新项目时,代码将添加到另一个带有.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;

有时设计器不添加此代码,因此在该项后面的代码中没有定义。