Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

SharePoint 2013 Visual Web Part Auto Generated Files

Brian Feldmann

As you may already know, SharePoint allows developers to create custom web parts a few different ways. You can create a web part through code using the Visual Studio Web Part Template or you can create it with a Visual Web Part Template. The Visual Web Part templates are nice because they allow you to use user controls (.ascx) files to generate your markup.

If you’ve ever created one of these Visual Web parts you’ve probably noticed that there is a code behind .ascx.cs file and another odd file with a ascx.g.cs extension. This .asxc.g.cs file is automatically generated by Visual Studio and is quite important. Every time you make an update or change the .ascx file and then press save, Visual Studio pushes those updates into this auto-generated file. The .ascx control is never actually deployed; instead the code files are packaged into an assembly and added to the GAC, creating the web part in SharePoint.

That’s all fine and dandy, but have you ever run into the issue where you’ve made some changes to the .ascx control, deployed the solution and the web part hasn’t updated? I can tell you that I’ve seen this happen more times than I’d like, but luckily there is a fairly simple fix for the problem.

Disconnected Web Part File

Visual Web Part Module with disconnected auto-generated file.

The issue, most likely, has to do with your auto-generated file somehow being disconnected from the .ascx control. If you load up your Visual Studio solution you may notice that the auto-generated file is no longer a child of the .ascx control. This prevents your changes from being pushed into the auto-generated file and ultimately deployed to SharePoint.

To fix the problem you need to open up and edit the project .csproj file. Simply open the file and search for your web part, in my case I’m looking for the “SiteDescriptionWebPart” web part.

csproj File - Disassociated Web Part

Problem Web Part; No longer associated with its parent .ascx control

Once, you’ve identified the web part elements, you’ll need to make the following changes:

  • add a <SubType>Code</SubType> node to the .ascx.cs compile element
<Compile Include="WebParts\SiteDescriptionWebPart\SiteDescriptionWebPart.ascx.cs">
  <DependentUpon>SiteDescriptionWebPart.ascx</DependentUpon>
  <SubType>Code</SubType>
</Compile>
  • add a <DependentUpon>SiteDescriptionWebPart.ascx</DependentUpon> node to the .ascx.g.cs compile element
  • add a <SubType>Code</SubType> node to the .ascx.g.cs compile element
  • add a <DesignTime>True</DesignTime> node to the .ascx.g.cs compile element
  • add a <AutoGen>Code</AutoGen> node to the .ascx.g.cs compile element
<Compile Include="WebParts\SiteDescriptionWebPart\SiteDescriptionWebPart.ascx.g.cs">
  <DependentUpon>SiteDescriptionWebPart.ascx</DependentUpon>
  <SubType>Code</SubType>
  <DesignTime>True</DesignTime>
  <AutoGen>Code</AutoGen>
</Compile>

Save the .csproj file and go back to the solution in Visual Studio. You will be prompted with a notification that changes have been made, click reload. Once the project has been reloaded the auto-generated file should be a child of the .ascx control. You’re now ready to make updates to your web part.

Visual Web Part Module with code behind and auto-generated files correctly associated

Visual Web Part Module with code behind and auto-generated files correctly associated

3 Comments. Leave new

Hi, I did the same way but still my .ascx.g.cs file not updating. help needed.

Thanks!

Yes, my file was not updating either, after going thru these steps. I found that you also need to check that the Custom Tool property of your .ascx file is set to “SharePointWebPartCodeGenerator” and then you may have to do more edits of the .csproj file. See for details.

Excellent , I searched a lot and tried different solutions and finally solved!!! Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\