Zectzozda
Zectzozda

Reputation: 77

Accessing textboxes, labels, etc from code-behind file

I have a Web Forms page that contains a label and two text boxes that I want to access in the page's code behind file, but when I call the text box in the code behind file, the red squiggly lines show underneath the label and text boxes call. I have tried the solutions mentioned in similar questions (regenerate the designer file, make sure that there are not multiple text boxes/labels with the same name in other pages, create a new aspx page and re-write the code) but none of them have fixed this scoping problem. This code compiled fine last night, I have not changed the code since then and it is not compiling.

I am relatively new to C#, so I may be missing something extremely obvious, for which I apologise in advance for.

In 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
    }
}

In 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>

The Login.aspx.designer.cs file :

//------------------------------------------------------------------------------
// <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;
    }
}

Upvotes: 0

Views: 2144

Answers (2)

Nick Mehrdad Babaki
Nick Mehrdad Babaki

Reputation: 12545

When you adding new items to your page automatically the code would be added to another file with .designer.cs for example when you put your Label on the default.aspx page you should see the code below on the 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;

Sometimes designer does not add this code so in the code behind this item is not defined.

Upvotes: 0

Abbath
Abbath

Reputation: 592

You have called your password box ID="txtBxPassword" in the html, yet in your code behind are trying to access it as txtBxPassword_Login

Upvotes: 1

Related Questions