Saturday, August 17, 2013

Fix bug in Visual Studio 2012 with deleting generated ascx.g.cs files for web parts

If you develop web parts in Visual Studio 2012 for Sharepoint 2013, you may notice that now for each web part VS creates files differently, then in VS2010 and Sharepoint 2010:

  • ascx file – contains layout of web part;
  • ascx.cs – contains partial class with your code;
  • ascx.g.cs – contains automatically generated partial class, which contains code representation of layout from ascx file.

The main difference with VS2010 is that automatically generated file contains all initialization code for all elements in ascx, not just declaration of protected controls (like ASP.Net does when request goes to aspx files). With this approach it is not needed to copy ascx files to ControlTemplates folder, because all web part code is now compiled into the assembly. With this approach it is easier to develop web parts for O365 sites.

There is one bug in VS2012: when you try to change ascx file for web part automatically generated ascx.g.cs file is deleted. In order to avoid this problem you need to specify url of the correct Sharepoint site in “Site URL” for the project with the web part in VS:


Seems like VS tries to connect to Sharepoint site, when you change ascx file.

Note that if you use host headers for your sites on local dev environment, then it is necessary to add these host headers to your hosts file:

Otherwise when you will try to save “Site URL”, you will see error “Remote SharePoint site connections are not supported”:


Hope that it will help you if you will encounter with this problem.


  1. I already set the site url but the problem still exist even when using VS2013 and i have source control when the file is removed i undo changes at TFS and return it back but it became not linked to the web part wehn i change the ascx file no the .ascx.g file is not updated again?? any idea how to solve this or link the restored file with the webpart again.

  2. hello
    try to also restore csproj file and reload the project

  3. I would suggest you execute a compare on your csproj file. You only need to bring over the compile elements for *.ascx.cs & *.ascx.g.cs, and verify the content element for *.ascx has LastGenOutput included otherwise it will create a *1.ascx.g.cs copy.