I've discovered a problem when a Visual webpart's g.cs appears to be empty. I'm using Visual Studio 2012 Update 3 and a project for SharePoint 2010.

The steps to reproduce the problem:

  1. in Visual 2012, create a solution with two projects. The first one contains a user control. The second - a Visual Webpart;
  2. drag-and-drop the user control to the Visual Webpart's ASCX markup.
  3. ascx.g.cs file turns out to be empty

I've tried the same steps on different environments and the results are the same.

User control inside a WebPart

I know there are several workarounds like adding an old Visual Webpart template to the Visual Studio or adding controls from codebehind etc.

I am curious if there is a fix for that? I am pretty comfortable with these visual webparts and I wish I could continue using them. Thank you in advance!

Update: As incredible as it seems, this problem is still present in Visual Studio 2013...

  • Just curious why would you drag and drop UserControl from another project when you can create one in same project and call it manually using code behind – Muhammad Raja Sep 30 '13 at 8:54
  • 1
    Well, I have a large SharePoint solution. All reusable user controls are stored in a separate SharePoint project. In fact, I already have a ready-to-use Project with several controls from previous projects. I don't really like the idea of recreating these controls inside a new project. Of course, I might end up doing just that... – Denis Molodtsov Sep 30 '13 at 9:05
  • Oh, I've just realized what you were asking. Actually I don't drag and drop the control itself. I just drag and drop the 'reference' to this control. Physically it stays in the other project. If you drag and drop any user control to the ascx markup Visual studio automatically registers this control and adds it to the markup. – Denis Molodtsov Sep 30 '13 at 9:25
  • I've same problem :( Did you find any workaround for this issue? – Sadegh Dec 7 '13 at 13:11
  • Well, I could not fix it in the end, But I've used a solution by Anatoly Mironov (chuvash.eu/2012/09/20/…). Basically, you can copy Visual Web part template from VS 2010 to VS 2012/2013. Also, I'm not sure, but maybe they've fixed it in VS 2013 already. – Denis Molodtsov Dec 7 '13 at 16:12
up vote 0 down vote accepted

As you mentioned, you can copy visual web part template from VS 2010 to VS 2012. IT doesn't look fixed in 2013 either, however I don't see this being fixed as a matter of urgency.

This may be unrelated, but did you put a proper URL in your project's Site URL field, and can you properly deploy and activate an empty solution from VS?

I find that the designer files are not generated properly if my site URL is faulty (VS can't connect to it) or if I lack the permissions to deploy and activate a solution from within Visual Studio.

  • Yes, this might be a real solution in some cases. This is the first thing to check. Unfortunately, there is also another bug unrelated to the site URL. – Denis Molodtsov Jan 9 '14 at 10:11
  • it fixed my problem when .g file was removed and do not gets updated. url in project property (f4) was incorrect and was rolled back to old server somehow. correcting this url fixed the problem – Iman Abidi Oct 26 '15 at 11:43

This one worked for me:

  • Highlight YourControl.ascx file in solution explorer.
  • In properties window locate "Custom Tool" property and set it to SharePointWebPartCodeGenerator
  • If your *.ascx file is valid, the ascx.g.cs should be generated as a result

In my case mentioned, the property was empty and there was no sign of a g.cs file.

This article is a workaround for this problem.

http://samer-othman.blogspot.pt/2013/03/how-to-fix-error-name-initializecontrol.html

  • 2
    Please, do not only provide a link as an answer. If the link breaks your answer will not be of much help to future readers. Add some details from the link, with a clear reference to the source off course :) – Robert Lindgren Dec 9 '13 at 11:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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