【问题标题】:Click Event Handler of ASP.Net Button in User Control is not Working用户控件中 ASP.Net 按钮的单击事件处理程序不起作用
【发布时间】:2016-08-02 18:56:49
【问题描述】:

我的 ASP.Net 应用程序中的用户控件有一个名为 button 的 Button。我已经像这样向它添加了一个点击事件:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        button.Click += (object o, EventArgs ea) => {
            Response.Redirect("~/Post.aspx?type=" + Type + "&id=" + PostID);
        };
    }
}

但每当我点击按钮时,它只会重新加载页面。我的代码有什么问题?

【问题讨论】:

    标签: c# asp.net button event-handling


    【解决方案1】:

    您应该删除if (!IsPostBack) 条件以在每次回发时设置事件处理程序:

    protected void Page_Load(object sender, EventArgs e) {
        button.Click += (object o, EventArgs ea) => {
            Response.Redirect("~/Post.aspx?type=" + Type + "&id=" + PostID);
        };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多