I am encountering this error when I create a visual web part in Visual Studio 2012:

the name 'initializecontrol' does not exist in the current context visual studio 2013 

Edit 1: the error appears in this line of code in .ascx.cs file:

protected override void OnInit(EventArgs e)

And there is no .g.cs file.

I have seen some blogs or posts, but they are not helping me with this issue. I have installed Updates 4 for visual studio. Hope to hear from you guys.

  • Is there a markup error in your aspx file? Does your .g.cs file exist and have content? Commented May 2, 2014 at 13:02
  • I just updated my post. Please have a look at Edit 1. There is no .g.cs file.
    – Imir Hoxha
    Commented May 2, 2014 at 13:16
  • There should be a .g.cs file or a designer.cs file. If there is not you may have an error in your markup. Commented May 2, 2014 at 13:18
  • I had this issue when i copied some code from another project. Turned out i missed to change the Inherits property (Namespece) in the ascx Commented Jan 15, 2015 at 14:50
  • I had turned off the connection to SharePoint, which turns off generating the .g.cs file, and when I turned it back on it still hadnt generated the .g.cs file until I had made a change to the .ascx page.
    – Nacht
    Commented May 26, 2015 at 7:15

6 Answers 6


I finally found one solution. It has all to do with the url of the site. It should be a valid url. It is a bit strange though because the url that I was providing was valid. To test it I created a new webapp with a site collection eg.: http://mysomething:5040 and visual web part was created successfully with the design file "....g.cs".


You are probably missing VisualWebpartName.ascx.c.gs file under your VisualWebpartName.ascx. Right click your VisualWebpartName.ascx and select Run Custom Tool . This will recreate .c.gs file and all errors will be gone.

  • I was also facing the same issue when tried to update code in existing Visual Webpart. But after running Custom Tool. Thanks Ergec. I think first check Site URL in project properties. Copy and paste the URL in browser and if it is correct then Run Custom Tool. Otherwise correct Site URL and then Run Custom Tool. Commented Mar 5, 2020 at 8:27

I had a similar problem today creating a new WebPart: I renamed the default webpart name (VisualWebPart1), but VS does not rename all the code automatically, only the files, and the control reference in the ascx file was using the old ".VisualWebPart1.VisualWebPart1" reference.

I just changed the namespace to the correct one, and it worked like a charm...


Try to add new user control with the web part name, it will create ascx.g.cs file. This will solve the problem. You can delete the usercontrol folder later.


I corrected the issue, for me it occurred since i have given the Site URL as only till farm level (http://w12devt) but actually it should be URL of any site collection (http://w12devt:9999/sites/rnd).


In my case it solved by adding the setting for hosts file ( The Deploy URL ) like mysite.domain.com

  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review
    – Waqas Sarwar MVP
    Commented Feb 24, 2016 at 11:28
  • 1
    It may help someone to solve the problem , This question above , I solved it using this way
    – Peter
    Commented Feb 24, 2016 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.