Serge P
Serge P

Reputation: 1611

"The name does not exist in the current context" in ASPX page

I've had a look at similar questions (1,2) but they don't appear to provide me a solution :S

I'm trying to add an extra field to a registration form that is already pre-built (i.e. comes with) in ASP.Net Web Form.

What I'm doing is, in the Register.aspx.cs backend I try to obtain the value txtCompanyInput.Text();:

    protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
       ... // some code here 

       String CompanyName = txtCompanyInput.Text();

       ... // other code here

       Response.Redirect(continueUrl);
    }

The Register.aspx itself looks like so:

    ...
    <li>
        <asp:Label runat="server" AssociatedControlID="ConfirmPassword">
           Confirm password</asp:Label>
        <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
        <asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
           CssClass="field-validation-error" Display="Dynamic"
           ErrorMessage="The confirm password field is required." />
        <asp:CompareValidator runat="server" ControlToCompare="Password" 
           ControlToValidate="ConfirmPassword"
           CssClass="field-validation-error" Display="Dynamic"
           ErrorMessage="The password and confirmation password do not match." />
    </li>
    <li>
           <asp:TextBox ID="txtCompanyInput" runat="server"></asp:TextBox>
    </li>
    ...

When I try to build the project I run into an error saying:

Error 1 The name 'txtCompanyInput' does not exist in the current context c:\users\...\Account\Register.aspx.cs 62 34 ProjectTest

Is there a reason why I'm not able to reference the txtCompanyName field in the backend?

Upvotes: 3

Views: 15639

Answers (2)

Danniel Little
Danniel Little

Reputation: 821

If you are using VS2013 update 5 like me, do these steps to regenerate the designer file:

  1. Delete the stated Register.designer.cs

  2. Highlight 'Register.aspx' (must do!)

  3. Go Menu->'Project', Click 'Convert to Web Application'

Right click on anything in the solution explorer will NOT give you choice to 'Convert to web application'.

Hope this helps some beginners.

Upvotes: 5

Serge P
Serge P

Reputation: 1611

Ehsan Sajjad was on the right track. The Register.aspx.designer.cs file was not getting updated.

I've finally figured out this was due to the Textboxs and Labels being placed inside the asp:CreateUserWizard block - which you shouldn't do. Even after manually adding the elements to the designer.cs file would throw an error when referencing the Textbox (Object reference not set to an instance of an object)

I should have included a bigger code segment from the Register.aspx file in my initial question.

Upvotes: 2

Related Questions