【问题标题】:ASP.NET event is invoked but throws errorASP.NET 事件被调用但抛出错误
【发布时间】:2018-10-26 04:45:57
【问题描述】:

这就是我的LoginControl 类的样子:

namespace WebApplication3
{
    public partial class LoginControl : System.Web.UI.UserControl
    {
        public event EventHandler ButtonLoginClick;

        protected void Page_Load(object sender, EventArgs e)
        {                
        }

        protected void btnLogin_Click1(object sender, EventArgs e)
        {
            ButtonLoginClick(sender, e);
        }
    }
}

在我使用控件的页面上,我有以下代码:

<myComps:LoginControl runat="server" ID="LoginControl" ButtonLoginClick="btnLogin_Click"></myComps:LoginControl>

当我点击登录按钮时,没有触发该事件,因为我收到以下错误

一个对象没有被实例化。

我猜它是在谈论事件。错误发生在我调用事件ButtonLoginClick 时。

【问题讨论】:

  • 嗨,谢谢。但是我做得对吗?我在 page_load 中尝试过: ButtonLoginClick = new EventHandler();但它需要更多参数
  • 有错字吗? btnLogin_click 和 btnLogin_click1

标签: c# asp.net events


【解决方案1】:

您需要在页面上将ButtonLoginClick="btnLogin_Click" 更改为OnButtonLoginClick="btnLogin_Click"

然后 asp.net 会将您的处理程序绑定到 LoginControl 事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 2017-11-01
    相关资源
    最近更新 更多