C#ImageButton Click事件未触发 [英] C# ImageButton Click event not fired

查看:59
本文介绍了C#ImageButton Click事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码优先:

public class ExtendedGridView : GridView
{
    private ImageButton m_btnPrint;
    public ImageButton PrintButton
    {
        get { return m_btnPrint; }
        set { m_btnPrint = value; }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        PrintButton = new ImageButton();
        PrintButton.Click += new ImageClickEventHandler(OnPrintButtonClick);
        PrintButton.AlternateText = "Print";
        PrintButton.ImageUrl = "../../Images/Print.png";

        TableCell cell = new TableCell();
        cell.Controls.Add(PrintButton);
        cell.ColumnSpan = this.FooterRow.Cells.Count;

        this.FooterRow.Cells.Clear();
        this.FooterRow.Cells.Add(cell);
    }

    public void OnPrintButtonClick(object sender, ImageClickEventArgs e)
    {
        // Do Stuff
    }
}

这段代码旨在用图像按钮替换页脚内容,并且可以做到这一点.计划是在同一个类中处理click事件,但我无法使用方法'OnPrintButtonClick',只是不会在此处中断.该页面似乎确实在回发.以前在此站点上对此有疑问,但我还没有看到答案.预先感谢

This bit of code is intended to replace the footer content with an image button, and it does this fine. The plan is to handle the click event in the same class but I can't get the method 'OnPrintButtonClick' to go, it just won't break there. The page does, however, seem to be doing a post back. There have been questions on this before on this site but I haven't seen an answer yet. Thanks in advance

推荐答案

这是页面生命周期问题.

This is a page lifecycle problem.

您要在 PreRender 阶段将 ImageButton 添加到页面中(并注册您的处理程序),该阶段在控制事件发生后已被派遣.因此,永远不会调用您的处理程序.

You're adding the ImageButton to the page (and registering your handler) during the PreRender phase, which occurs after control events have been dispatched. Therefore, your handler will never be called.

如果可能的话,请尝试在 Load 阶段添加按钮.

Try adding the button during the Load phase, if at all possible.

这篇关于C#ImageButton Click事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆