2011-07-20 48 views
1

代码第一: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方法去,它不会在那里突破。但是,该页面似乎正在回发。在这个网站上有过这个问题,但我还没有看到答案。 在此先感谢

+0

OnPrintButtonClick被连接在哪里? –

回答

1

这是一个页面生命周期问题。

要添加的ImageButton页面(和登记处理)在PreRender阶段,发生控制活动已出动。因此,你的处理程序永远不会被调用。

尝试在Load阶段添加按钮(如有可能)。

+0

我覆盖OnLoad而不是OnRender – zXynK