ASP.Net GridView RowCommand did not get trigger

I was stuck in a GridView RowCommand problem for a long time. Not sure why the RowCommand did not get trigger…
Here is my situation:

I have a Grid View which is used to display a table with data, It also contain an image button which allow user to click on, to download a file.

I have done the similar feature several time, all of them works fine. Unfortunately, when the user click on the image button for this grid view, it just refresh the page, but it did not do anything. I try to check what have done wrong by comparing the code that I did before. They all looks fine.

So I have set up several break point to where the RowCommand handler is, a strange thing happened. The RowCommand Handler did not get triggered at all.

Finally, I just realised I have forgotten to put an if-statement ispostback on the Page Load event, so that whenever the RowCommand get triggered, it executed a post back, which will run the page_load event. And inside my Page_Load event, it will populate the table again. Therefore, the RowCommand never get triggered.

Solution is:
To put a code on Page_Load Event to check if it is a PostBack or not. If it is not a postback then populate the table, if Not, then skip it.
Here it is the Code to check if it is postback or not.

If Not IsPostBack Then
‘Do some action, in my case populate the table

//Do some action, in my case populate the table

Hope you find it useful! = )

Leave a Reply