I’ve ran into this error several times now.
The name ‘InitializeControl’ does not exist in the current context
There have been several suggestions on how to fix this, some blogs say to create a new Visual Web Part in the Template folder and copy it back into the Web Part Project and a myriad of other mumbo jumbo.
I discovered the cause of the problem by reading a connect.microsoft submission, some MSDN lit and a blog post. My apologies to the blogger, I can’t seem to find your site again.
Anyway, back to the cause and effect. The problem starts with the fact that the ascx.g.cs file is missing from one or more Visual Web Parts.
Okay, but why is the ascx.g.cs file missing? Well, it can happen for a couple reasons, mainly because VS is unable to contact the SharePoint environment.
The first thing we need to check is the Site URL property. Do this by selecting the project in the Solution Explorer and viewing the Project Properties. (This isn’t the right click properties method… I’m talking the properties window) Got it? Okay, verify the Site URL is correct by navigable by using your browser. In my case SharePoint was wrecked and throwing an error, not a web part error, just a general configuration type of error.
Right, weird… If you’re like me, this fixed the problem but leaves you with more questions than answers. This is where connect.microsoft and MSDN come in. It appears that the ascx.g.cs is actually generated by SharePoint and not Visual Studio and when you add Visual Web Parts and Visual Studio isn’t able to contact SharePoint the Visual Web Part is created but the component model is never created.
I tried rebuilding the g.cs file several different ways with no luck. I has to backup the code and HTML/ASP.NET into NotePad, delete the Visual Web Part and create a new.
I caught this on my first compile and had only created 1/2 a dozen classes and maybe 100 lines of code.
This will also cause the problem.
We contract most of our web design, I use a guy named Ryan over at http://adaptdesigns.net/ he does great work. He creates the HTML with the appropriate CSS and JS dependencies and hands the code over to us. I take the code and convert it into controls where necessary.
This is what I get from him.
<button class=”btn btn-primary”>Save changes</button>
I convert it to an ASP.NET control.
It should look like this.
<asp:button CssClass=”btn btn-primary” runat=”server” ID=”btn_New_SavePathway” OnClick=”btn_New_SavePathway_Click” Text=”Save changes”></asp:button>
Visual Studio even tells you it doesn’t like it but it lists it as a Warning, its a Warning that’s going to cause an error.
HTML Input tags with runat=”server” property also fail.
<input class=”form-control” id=”container-search” type=”text” runat=”server” placeholder=”Enter name or ID” value=””>