18

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...

5
  • 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 Sep 30, 2013 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... Sep 30, 2013 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. Sep 30, 2013 at 9:25
  • I've same problem :( Did you find any workaround for this issue?
    – Sadegh
    Dec 7, 2013 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. Dec 7, 2013 at 16:12

4 Answers 4

1

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.

4

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.

2
  • 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. Jan 9, 2014 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
    Oct 26, 2015 at 11:43
2

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, Custom Tool property was empty and because of that there was no sign of a g.cs file.

-1

This article is a workaround for this problem.

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

1
  • 3
    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 :) Dec 9, 2013 at 11:12

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.