Wednesday, April 17, 2013

SharePoint : Visual Studio InitializeControl() compilation error

At times Visual Studio 2012 visual webpart failes to compile all of a sudden. Example of such errors is the following screen shot.

I have about 2000 lines of HTML in .ASCX of visual webpart. Visual Studio generates ascx.g.cs file where all the designer information is auto generated. One of the reasons for this behavior is that if there is any HTML error, instead of erroring on a particular error this file fails to be generated and even after everything is fixed in HTML, Visual Studio cannot generate this file any more.

Here is one solution that could fix.

Save HTML and code behind in text files somewhere and delete the webpart by copying the name. Close Visual Studio, reopen and recreate the webpart with the same name. Once webpart is created, copy HTML and code behind from text files saved earlier and rebuild the project.

Make sure webpart is created with the same name as it was earlier because this webpart might have been referenced at many other places.

I have seen some blogs where if code execeeds certain number of lines Visual Studio has an issue to build. In my case, there weren't so many lines so I could not reproduce the error.