19139

How can I remove ASP.NET Designer.cs files?

I've worked on VS projects before where there is no .designer.cs files.

Now I started a new project on a different computer and I can't get rid of designer.cs files. It's really annoying me. Do I really need it, how can I remove it? There must be a setting somewhere.

Answer1:

You're dealing with a web application rather than a website (clarification)

Yes, in the context of a web application, you do need them.

Answer2:

YES! You can remove them......here is how.....

HOW TO DELETE DESIGNER.CS PAGES FROM YOUR WEB APPLICATION

After great torture and much testing seeking a way to avoid designer.cs pages in Visual Studio (v. 2015 and earlier), I finally found a work around for this. If anyone is stuck with designer.cs pages in a <strong>Web Application Project</strong> in Visual Studio this solution will allow you to erase all application compile errors quickly then delete the designer.cs pages completely from your project.

First understand the following:

<strong>Web Application Projects</strong> use designer.cs pages (partial classes) auto-generated by Visual Studio, and which are tied to the design tools built into Web Applications which the web app model sustains. I call it web sites for dummies. I could find no setting or way to turn the creation of partial classes and designer.cs pages off, as they are often unnecessary code tied more to the IDE than the functioning of the application. All partial classes get compiled into one class anyway. Web Apps also get pre-compiled or built ahead of time in general by design, and those dll's gets pushed into bin folders.

<strong>Web Site Projects</strong> do not use designer.cs files and are less tied to the IDE. They allow for a cleaner coding of class structures. They also use partial classes. But Web Sites Projects generally get compiled at runtime.

SOLUTION - How to Remove "...designer.cs" errors and files from a Web Application Project in Visual Studio.

Unless you want to convert your web project from an Application to a Web Site model in Visual Studio, this solution below allows you to run your project as is, yet like a Web Site, where you can move most or all the designer.cs partial class control references from that file into your main web page partial class file. It also removes all errors AND does not interfere with the Visual Studio recreating those designer files should other developers be sharing them and adding controls to pages and forget to use this solution on specific pages.

<ol> <li>

In the front-end .aspx pages, in the @PAGE directive at the top, change the "CodeBehind" to "CodeFile". Make sure it still references the same .cs code or class file.

</li> <li>

Add the "CodeFileBaseClass" attribute to the same @Page directive in your web page and have it access the fully qualified path to the same .cs above with any namespaces in the path.

</li> <li>

Make sure you use the "Inherit" attribute with the same path as the "CodeFileBaseClass".

</li> </ol>

You should have the following for every web page in your Web Application with these attributes formatted as such:

<%@ Page Language="c#" CodeFile="index.aspx.cs" CodeFileBaseClass="YourNameSpace.index" Inherits="YourNameSpace.index" %>

Now go into your designer.cs file for the page and copy any control references from the partial class in your designer file into the partial class of your main .cs file for the web page. If you have lost your designer.cs files, just add in any web controls as field references as fields that the compiler says are missing. After you do that DELETE the designer.cs file. You don't need them and have complete control over your web page controls using the main .cs file and its partial class.

Below is what I had in the designer.cs file before deleting it. Below it is the main index.aspx.cs file for my project after I added the designers.cs control reference as a field in its partial class. I then deleted the designer.cs file and its code completely from my project:

namespace YourNameSpace { public partial class WebForm1 { /// <summary> /// form1 control. /// </summary> /// <remarks> /// Auto-generated field. /// To modify move field declaration from designer file to code-behind file. /// </remarks> protected global::System.Web.UI.HtmlControls.Literal Message1; } }

In my index.aspx.cs file below you can see where I pasted the last "Message1" reference line from the designer above into its partial class at the top. You can see how I was then able to access the web page Literal control, Message1, in my Page_Load event and modify the text. That shows it was referenced correctly now, compiled, and worked, where before such references failed if the designer.cs file was missing or its partial class failed:

namespace YourNameSpace { public partial class index : System.Web.UI.Page { protected global::System.Web.UI.WebControls.Literal Message1; protected void Page_Load(object sender, EventArgs e) { this.Message1.Text = "hello world"; } } }

The key to fixing this is the CodeFileBaseClass and CodeFile attributes in the @Page directive in the main web page. This prevents new member references and controls in the web page from being automatically generated by the Visual Studio IE and stuck as stubs in designer page partial classes (which to me is not helpful when you want full control over those fields in a single convenient area).

Note: If you later regenerate the designer.cs files again for your project by selecting your project in VS, then in the top menu choosing Project > "Convert to web Application" those designer.cs files will get recreated, BUT Visual Studio will ONLY place references to missing web controls in them that are NOT now added to your main .cs partial class page. In other words, any references I copied and added above to my index.aspx.cs partial class file would not be recreated in the new designer.cs file. So there is no conflict with the Web Application model you are using. When you see those designer files, you can now have developers safely copy them into your main code behind .cs files and safely delete the designers if you like.

Recommend

  • Xamarin Studio code completion and navigation not working
  • SSO, using Google Apps user database
  • How to find via Python the icon associated with a Windows file?
  • Compress html output from zend framework 2
  • PBXCp error no such file or directory
  • Cannot access vm using GCloud Compute
  • Why String.ToString()? [duplicate]
  • Run/Debug Maven JavaFX 2 in Netbeans
  • Where and How To Define An Application Property? - JHIpster
  • Load balanced Fiware Orion
  • How to attach php documentation in eclipse
  • How to add an object in my collection by only using add method? [closed]
  • How to blur the specific part of image without using Render Script,as my minsdk is 14.
  • GRPC and types import error in App Engine Datastore
  • How to use SBT with multiple sub project web applications?
  • Autohotkey script running program with command line arguments
  • Create registry key in 32-bit hive on x64 PC using Installshield 2012 LE - Avoid redirection
  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • Is there a way to call library thread-local init/cleanup on thread creation/destruction?
  • Zend Framework bassed projects
  • Setting WPF Window Background to Resource Dictionary Brush User Setting
  • Debugging VB6 Code From Visual Studio 2010
  • Alternative to overridePendingTransition() - Android
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Cannot resolve symbol 'MyApi'
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Adding custom controls to a full screen movie
  • Why winpcap requires both .lib and .dll to run?
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Memory offsets in inline assembly
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal