During SharePoint 2013 visual webpart development, some time VS 2013 will miss or delete the .g.cs and during compilation, we may get the errors like “InitializeControl does not exist in current context” .
To generate a new webpartusercontrol.ascx.g.cs file we need to follow the below steps
1. Unload the SharePoint Project
2. Right click the project and Unload the project(Edit the .csproj file)
3. Find the webpart where the .g.cs file is missing
4. Check the below XML Tag in that place, if its missing or “LastGenOutput” is missing then add accordingly
<Content Include=”Webparts\WP_TestWebpart\WP_TestWebpart.ascx”>
<Generator>SharePointWebPartCodeGenerator</Generator>
<LastGenOutput>WP_TestWebpart.ascx.g.cs</LastGenOutput>
</Content>
5. Save the .csproj file and load the project
6. Now Right click on the .ascx file and Run the Custom Toll. (Make sure in the properties of ascx , customTool is SharePointWebPartCodeGenerator)
7. Now the webpart .g.cs file will be generated and we can compile without any errors
I hope this may help someone who struggle or delete the webpart again , to generate the .g.cs file 🙂