我遇到以下错误:
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compile Error Message: CS1061: 'codebehind' does not contain a definition for 'btnSave_Click' and no extension method 'btnSave_Click' accepting a first argument of type 'codebehind' could be found (are you missing a using directive or an assembly reference?)
ASP按钮:
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-success btn-md" Width="100px" OnClick="btnSave_Click" />
事件注册在后面的代码中:
override protected void OnInit(EventArgs e)
{
btnSave.Click += new EventHandler(btnSave_Click);
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
OnClick事件:
private void btnSave_Click(object sender, EventArgs e)
{
//foo
}
我尝试过的事情:
相关问题供参考:
如果您需要其他信息,请告诉我。
答案 0 :(得分:2)
使按钮单击方法为protected
。
protected void btnSave_Click(object sender, EventArgs e)
{
//foo
}
但是为什么OnInit中的所有代码呢?您可以在其中添加Click事件,也可以在Button本身上添加它,因此这是多余的。您可以删除OnInit
和InitializeComponent
,它们仍然可以使用。