38

I've added a new control to my aspx files and noticed that not only was the new control not added to the designer file but that it was also missing quite a few other controls that were added by other members of the team. I've tried deleting the designer.cs file and using "Convert to Web Application" with no success. Some other things i've tried have been excluding the aspx from the project, building, and then re-including with no success. I've also manually entered in a control that was missing in the designer into the designer. When I run after do so an error appears saying the control isn't defined, even though it actually is, and that I should check if I'm missing a directive.

The first control I added was copy and paste from a similar control and made necessary changes. But, i've also tried manually creating the control with the same results.

Any ideas?

3
  • 1
    what version of visual studio are you using? Jun 28, 2011 at 14:41
  • 3
    Don't you just hate it when the Visual Studio magic flakes out!? Jun 28, 2011 at 14:42
  • Visual Studio 2010 .NET Framework 4
    – user819357
    Jun 28, 2011 at 14:46

20 Answers 20

42

Try this

1.- Change CodeBehind="Name.aspx.cs" to CodeFile ="Name.aspx.cs"

2.- Build

3.- Change CodeFile ="Name.aspx.cs" to CodeBehind ="Name.aspx.cs"

4
  • Thanks. That was easy Jul 20, 2017 at 3:53
  • Wow. Been seeing this issue for years and this is hands down the easiest solution. Just sucks that it has to be done. Nice share! +1
    – Nugs
    Jan 18, 2019 at 22:54
  • 2
    For me I need to clean, change CodeBehind to CodeFile, build, clean, change codefile to codebehind, build
    – Iannazzi
    Nov 1, 2019 at 15:08
  • This is awesome, this was my issue but for a different reason. I am converting a website project to a web application project. I didn't realized in the Website project it references CodeFile. once I changed it to CodeBehind, it started working.
    – greektreat
    Jan 15, 2021 at 17:18
21

I've been suffered this problem in my work.

The surest thing is you regenerate your .designer.cs file, and here is the solution:

  • Locate the corrupted aspx.designer.cs file through the Solution Explorer
  • Delete only the designer.cs file from your project
  • Right click your main aspx file and select “Convert to Web Application“.
6
  • It worked. Thanks! I might add that I followed this procedure in VS 2012. Why am I still working on webforms apps? That's the real question. Mar 25, 2014 at 21:38
  • 11
    In VS 2013 the option has inexplicably been moved to the Project drop-down menu on the menu bar. link May 7, 2014 at 12:54
  • 2
    The link is dead, there is a reason its not recommended to post links, any way the solution in the link is to remove the designer file then right click Project and select Convert to Web Application.
    – Peter
    Aug 7, 2014 at 12:10
  • 1
    But why is the designer file not updating by itself? Jun 28, 2016 at 6:39
  • The link is missing. There's no content in this post, whatsoever.
    – jpaugh
    Aug 23, 2017 at 12:46
14

For me the solution was to:

Put your page in design view and right click / refresh. It will sync the controls with the designer.cs. Make sure designer.cs is close before doing this.

Source

0
9

Close Visual Studio, then delete Temporary ASP.NET Files from C:\Windows\Microsoft.Net\Framework\Your_.Net_Version\ProjectName Delete the folder ProjectName and re-start Visual Studio. I had a similar issue some time ago and it was solved by these actions.

If you use IIS, you may need to stop the server/site to be able to delete the temp files.

1
  • 1
    this was the only thing that worked for visual studio 2013
    – Miguel
    Sep 28, 2015 at 13:37
7

To replace the designer file in VS 2013:

  1. Delete the old *.designer.cs file
  2. select the *.aspx file
  3. Select “Convert to Web Application“ from the Project top drop-down bar
0
4

I did this in VS2012:

  1. Open the page.aspx.designer.cs Find a control declaration of the same type of control (ie LinkButton)
  2. Copy it to the end of the file (before the end of class curly bracket)
  3. Rename the control variable to match the name of your control.

That's it.

1
  • 1
    sometimes doing it the manual way is the only options that work Jun 5, 2020 at 8:33
4

In my case, I had to add the runat="server" attribute then to rebuild the solution. The missing elements were automatically added to the generated aspx.designer.cs and become available for use in code

<button runat="server" id="Button01" type="button" class="Button" onclick="location.href='http://http://www.example.com/';">
   <asp:Label runat="server" Text='<%# GetText("Button01")%>'></asp:Label>
</button>
3

One simple/stupid mistake that also results in similar symptoms is if you copy aspx and aspx.cs files and rename them, but neglect to update the references inside the file.

I came across this question trying to find the temp asp.net directory, but clearing that didn't help. I then realized that I did not update CodeFile="Page.ascx.cs" after I had copied the files to create a new modified version of the page. Then i was trying to add a control and reference it in PageModified.ascx.cs but kept saying it didn't exist.

1
  • Bingo. It was still referencing the file I copied from.
    – goodeye
    Mar 19, 2014 at 23:13
1

This also seems to happen when you have a usercontrol that references another usercontrol in the same namespace. if you move the referenced user control to a different namespace the problem goes away

1

Sometimes there might be errors in html like two controls having the same Id. Also be careful with div and span tags. These stops the designer from getting updated. For all of these, check the warnings in Error List and fix them. You are ready to go. This solved my issue.

1

I was able to solve the problem only manually adding the control declaration inside the code behind file. Every time I tried to regenerate the design.cs file, VS 2013 would write the same lines without the missing control.

The declaration needs to be added in the code behind, NOT in the design.cs, because it would be deleted each time the .aspx is changed.

Just add the declaration like you would do for any other variable, something like this:

protected MyNameSpace.MyControl ControlName;
0
1

Just to add to the list of possible solutions: These lines in my markup file, although seemingly valid, were apparently causing a disconnect with the designer file:

<%: Styles.Render("/my-file.css") %>
<%: Scripts.Render("/my-file.js") %>

I simply commented them out, saved, and the designed file began working correctly. I then uncommented both lines, saved, and was able to add new controls to the page without issue. I found this strange, and it's still unclear to me what the initial cause of the problem was. I can only speculate that it's a Visual Studio bug.

Point being, there may be perfectly valid code that's causing an issue, in which case, a possible solution would be to go through commenting HTML sections out in your markup file until the designer file resolves itself, and then you can undo all your commenting and resume working as normal.

1
  • Thank you from VS PRO 2019
    – Fus Ro Dah
    Jan 21, 2022 at 20:54
1

For creating a code-behind C# file with Visual Studio 2019 from inline ASPX code, I had success with the following procedure:

  • Create CS file with the same base name and '.cs' extension, using Project/AddClass from the main menu.

  • Move all C# code from the ASPX file into this new file. Add the following code to this new class:

    namespace <<your_namespace>> { public partial class <<your_class_name>> : System.Web.UI.Page {

  • Add / modify the 1st line of the ASPX file:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="<<your_class_name>>.aspx.cs" Inherits="EVV.pages_sqlserver.<<your_class_name>>" %>

  • Create CS file with the same base name and '.designer.cs' extension, using Project/AddClass from the main menu.

  • Copy the contents of any other .designer file. Remove the complete contents of the class, only curly brackets remain.

  • Open the form designer for the basic ASPX file. Right-keep any empty area and select Refresh. Sometimes, this is sufficient. If not:

  • In Solution Explorer right-click the same ASPX file and select Publish.

  • Sometimes it only worked after closing Visual Studio, deleting all temporary files and reopening the solution.

** Update: in Visual Studio: 16.6.3 this seems to be broken: it sometimes works, sometimes not. Even for completely new WebForms, there is no update on the Designer file. **

** Update 2: This does not work reliably. I switched to Redesigner (https://github.com/seanofw/Redesigner/tree/branches/net40) which is working like a charm.

0

My designer.vb code file was not updating because of an error in Telerik web controls - I had an incorrect value in the DataKeyNames attribute of a MasterTableView. The aspx editor did not pick this up at design time as it's simply a string value, but it was causing the autogeneration of the designer.vb file to fail.

0

I tried all of the above in VS2019.

I deleted the designer file, clicked on the Project Menu / Convert to Web Application which rebuilt the designer file, however, it still wouldn't auto regenerate.

What worked - excluded the file from the project, then reincluded it.

The file now keeps auto regenerating.

0

In my case it helps just to change "ID" attribute of control in ascx file to some other value first, and then back to desired. It leads to refreshing of ascx.designer.cs file. Simple trick in case C# code is not generated for your control yet for some reason...

0

For me the solution was:

  1. I deleted the designer file.
  2. Recycle bin and restore the same file.
  3. In visual studio solution explorer, click on show hidden items from top and include the same file to the ascx file.
  4. Right click on ascx page and click on view designer. (optional)
  5. On the designer, right click and refresh and save. (optional)
0

As a solution right clicked on the aspx.cs page and selected move... into and choose the

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 12, 2021 at 2:18
0

Make sure your control is in the page's scope. In my case, it was in a Repeater template.

0

What worked for me is that I deleted all obj and bin folders. Rebuilt and it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.