.ascx.g.cs文件不能生成 The name ‘InitializeControl’ does not exist in the current context - Visual Web part Sharepoint

InitializeControl doesn't exsit

When using visual studio 2012 for developing
SharePoint 2013 Visual web parts either of type sandbox solution or farm
solution you might face a the following error:

“The name InitializeControl” does not exist with
the current context”

.ascx.g.cs文件不能生成    The name ‘InitializeControl’ does not exist in the current context  - Visual Web part Sharepoint
 
Before describing the solution lets first describe
the error, as shown in the image above this error occurs on the OnInit
Method.
About OnInit: OnInit
method is part of the web part life cycle, which is responsible for
initialization the controls inside the web part (as example label, button, textbox and
others).
By mapping between the error and the OnInite method
you will realize that this issue relates to controls inside the web
part.
By checking the visual web part, I found that the
designer file of type “ascx.g.cs” is missing, this file is responsible for
drawing the controls that are mapped with the visual web
part.
 
.ascx.g.cs文件不能生成    The name ‘InitializeControl’ does not exist in the current context  - Visual Web part Sharepoint

This issue
could occur by two reasons, as follows
1.The
solution is not using an existing or valid URL, by checking the SharePoint URL
please make sure that the URL is valid and exists, try to map the URL and build
your solution and check the result if the error still
exists.
 
2.If
the error still exists and the URL you are using is correct then we will do some
manual steps on top of visual studio as follows
a.Right click on you SharePoint project and then add
new item
b.Add
user control as shown below, its better to name this user control by the same
name of your visual web part, as example if your web part called Visual Web part
1, then for the User control name it as Visual Web part 1
 
.ascx.g.cs文件不能生成    The name ‘InitializeControl’ does not exist in the current context  - Visual Web part Sharepoint
 
 

c.The
user control will resides under the ControlTemplates folder (Mapped folder) as
shown below

.ascx.g.cs文件不能生成    The name ‘InitializeControl’ does not exist in the current context  - Visual Web part Sharepoint
 
d.Under the visual web part “VisualWebPart1” make
sure to delete the “VisualWebPart1.ascx” as shown below
 
.ascx.g.cs文件不能生成    The name ‘InitializeControl’ does not exist in the current context  - Visual Web part Sharepoint
 

e.
Then
we will drag the VisualWebPart1.ascx that resides in the ControlTemplates folder
and drop it under the VisualWebPart1 , this will add the new user control
including its designer under the VisualWebPart1, your solution should look like
below

.ascx.g.cs文件不能生成    The name ‘InitializeControl’ does not exist in the current context  - Visual Web part Sharepoint

f.  
Now try to add “Label” as example and build your solution, the result is
great and the error has gone.

The Question is: do we need to
repeat same steps for any new visual web part?
The answer is No, simply you can remove
the old web part , then add a new visual web part and the Visual Studio will
automatically create the “ascx.designer.cs” file for you ,so consider your
visual was stuck but then it works properly.

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

上一篇:【OpenCV入门教程之一】 安装OpenCV:OpenCV 3.0 +VS 2013 开发环境配置


下一篇:php部分(查看文件、建立站点、语法变量、变量的几个方法、“全局局部变量的调用”、static、函数参数的作用域);