1

Code first:

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
    }
}

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

1
  • Where is OnPrintButtonClick being wired up? Commented Jul 20, 2011 at 13:27

1 Answer 1

1

This is a page lifecycle problem.

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.

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

Sign up to request clarification or add additional context in comments.

1 Comment

I overrode OnLoad instead of OnRender

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.