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

本文详细介绍了如何解决在使用 Visual Studio 2012 开发 SharePoint 2013 Visual Web Part 时遇到的 ‘ThenameInitializeControl’ 错误。主要涉及到 OnInit 方法中初始化控件的问题,通过检查并修复缺少的设计文件来解决问题。

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


 

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.

 


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
 

 

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


 

d.Under the visual web part “VisualWebPart1” make sure to delete the “VisualWebPart1.ascx” as shown below

 


 

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





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


转载于:https://www.cnblogs.com/kinpauln/p/3287614.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值