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:
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.
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:
- Right-click the visual web part (.ascx) that is causing the problem, and select Properties.
- Ensure that the “Custom Tool” property is set to “SharePointWebPartCodeGenerator”.
- Right-click the visual web part again and select “Run Custom Tool.” This should re-generate the “.ascx.g.cs” file.
Done. Good luck!