30

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?

user819357
  • 301
  • 1
  • 3
  • 4

17 Answers17

32

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"

jpaugh
  • 5,719
  • 4
  • 33
  • 83
  • Thanks. That was easy – Varun Babu Pozhath Jul 20 '17 at 03: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 '19 at 22:54
  • 1
    For me I need to clean, change CodeBehind to CodeFile, build, clean, change codefile to codebehind, build – Iannazzi Nov 01 '19 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 '21 at 17:18
15

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
  • Rightclick your main aspx file and select “Convert to Web Application“.
Philippe
  • 532
  • 8
  • 23
SUN Jiangong
  • 4,779
  • 16
  • 53
  • 75
  • 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. – MrBoJangles Mar 25 '14 at 21:38
  • 8
    In VS 2013 the option has inexplicably been moved to the Project drop-down menu on the menu bar. [link](http://connect.microsoft.com/VisualStudio/feedback/details/806283/regenerate-the-designer-file-filename-aspx-designer-cs-in-visual-studio-2013) – Steve Young May 07 '14 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 07 '14 at 12:10
  • 1
    But why is the designer file not updating by itself? – Anders Lindén Jun 28 '16 at 06:39
  • The link is missing. There's [no content](https://meta.stackexchange.com/q/225370/220651) in this post, whatsoever. – jpaugh Aug 23 '17 at 12:46
  • Excellent Solution. The designer file must be removed through visual studio for this to work. Removing it from File Explorer will not work. – Ben Apr 13 '20 at 20:23
10

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

bombek
  • 335
  • 3
  • 20
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.

Ken D
  • 5,580
  • 1
  • 32
  • 56
6

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
Jake Hunter
  • 121
  • 1
  • 2
  • 8
3

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.

Papa Stahl
  • 449
  • 5
  • 7
3

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>
Moslem Ben Dhaou
  • 6,541
  • 6
  • 55
  • 85
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.

Thymine
  • 7,739
  • 1
  • 28
  • 42
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

Leo
  • 1,355
  • 22
  • 38
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.

vanitha
  • 17
  • 1
  • 7
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;
f.cipriani
  • 2,804
  • 2
  • 23
  • 21
1

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.

Adam
  • 363
  • 2
  • 8
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.

cskwg
  • 527
  • 5
  • 9
0

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.

StronglyTyped
  • 2,016
  • 3
  • 25
  • 47
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.

PRS
  • 667
  • 7
  • 22
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)
Bimal Das
  • 1,682
  • 4
  • 25
  • 47