Designer.cs not updating when new controls added to .aspx

Viewed 65911

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?

19 Answers

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“.

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

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>

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.

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.

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.

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.

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

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)

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

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

Related