The dreaded “The name ‘InitializeControl’ does not exist in this context” error when working with Visual Web Parts in Visual Studio 2012
Sharing Our Expertise

Our consultants solve challenging business problems and love sharing their knowledge. Tap into our insights and put them to work for you.

Back to Blogs >>

Published by Chris Johnson, on Monday, March 31, 2014

This happens to me all the time when I’m working with Visual Web Parts in Visual Studio 2012, and I finally found a good solution here to fix it, which I summarize briefly below:

Problem

You’re building a Visual Web Part in Visual Studio 2012, and all of a sudden it fails to Build, with this error:

The name ‘InitializeControl’ does not exist in this context.

This usually happens when the “Site URL” property on your SharePoint project is pointing to an invalid or nonexistent site (don’t get me started about how stupid it is that VS can’t detect this and tell you…).  The reason the build fails is because VS needs to use the existing site to compile the “.ascx.g.cs” file that sits behind the “.ascx” visual web part.

Unfortunately, simply setting the Site URL property on the SharePoint project to a valid site doesn’t fix the issue.

Solution

I have tried several methods to fix this (this one worked, but it’s a pain).  I finally stumbled across this page, which explains (in a somewhat roundabout fashion, which is why I want to credit this author but summarize the steps more concisely) that the “.ascx.g.cs” file can be regenerated with the following steps:

  1. Right-click the visual web part (.ascx) that is causing the problem, and select Properties.
  2. Ensure that the “Custom Tool” property is set to “SharePointWebPartCodeGenerator”.
  3. Right-click the visual web part again and select “Run Custom Tool.”  This should re-generate the “.ascx.g.cs” file.

Done.  Good luck!



Categories: SharePoint,.ascx,InitializeControl,PSC,sharepoint,user control,web parts

Original Post: https://thedotnetter.wordpress.com/2014/04/03/the-dreaded-the-name-initializecontrol-does-not-exist-in-this-context-error-when-working-with-visual-web-parts-in-visual-studio-2012/

Recommended For You
Join Mark Roden for "Using VSTS to automate build and deployment tasks for SharePoint Framework webparts" at SharePoint Fest Chicago on Dec 7.
View More ...
What do email-enabled apps look like in the modern (cloud) workplace? In this multi-part series, I try to answer that very question– beginning ...
View More ...
I use it all day, every day at work. I also help run my office Fantasy Football league. See where this is headed? This year, I decided to run our...
View More ...
Confidence in info security and access gets a lot of attention when using SharePoint Online. Here's how to set it properly for your requirements.
View More ...