3

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?

Community
  • 1
  • 1
Serge P
  • 1,591
  • 8
  • 22
  • 42
  • 4
    control is not added in the Register.designer.cs – Ehsan Sajjad Jun 03 '14 at 06:37
  • 1
    Please check the designer file! It may be the culprit! – Srinivasan MK Jun 03 '14 at 06:38
  • I see, that did it! Added `protected global::System.Web.UI.WebControls.TextBox txtCompanyInput;` to the `Register.aspx.designed.cs` file – Serge P Jun 03 '14 at 06:45
  • I'm trying to understand what could have caused this issue. Can someone throw light in this? Normally you wouldn't need to edit desinger files manually most of the time. A build after adding the control in the aspx file would be sufficient, wouldn't it? – LakshmiNarayanan Jun 03 '14 at 08:08
  • I've had to add controls manually before, it's annoying, but normally its because something else in your project is not right. Also a restart of Visual Studio sometimes clears this up. – matt_lethargic Jun 03 '14 at 10:23

2 Answers2

4

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.

Danniel Little
  • 746
  • 7
  • 12
2

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.

Community
  • 1
  • 1
Serge P
  • 1,591
  • 8
  • 22
  • 42