ImageButton in DataGrid and DataGrid.ItemCommand Event

I recently had an ImageButton in a DataGrid, and for whatever reason it wouldn’t fire the DataGrid.ItemCommand event.  After beating my head on the keyboard for an entire day, I finally found the problem.

When you are binding the grid (or calling the method that handles the binding) in the page_load, you have to make sure that you include it in a not postback check.  The ItemCommand event is a postback event, so if it’s just hanging out in the page load event, it will always rebind the grid when the page is loaded and destroy any postback events.

That’s what I was doing… doh.

    private void Page_Load(object sender, System.EventArgs e)


        // Only perform if the page is not a postback (initial load)

        if (!Page.IsPostBack)


            // LoadFiles method handles our datagrid’s binding




Comments (0)

Skip to main content