ASP.NET Web窗体ASP按钮OnClick编译器错误(CS1061)

雅各布·罗素

我收到以下错误:

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
    }

我尝试过的事情:

  • 在视图和代码隐藏中重命名onClick事件
  • 删除onClick,转到设计标签,双击按钮以自动生成新事件
  • 将自动事件联结从false更改为true

相关问题供参考:

如果您需要其他信息,请告诉我。

虚拟世界

制作Button Click方法protected

protected void btnSave_Click(object sender, EventArgs e)
{
    //foo
}

但是为什么OnInit中的所有代码呢?您可以在其中添加Click事件,也可以在Button本身上添加它,因此这是多余的。您可以删除OnInitInitializeComponent,并且仍然可以使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章