Wednesday, 26 August 2009

OnRowCommand fires when GridView sorting

When sorting is enabled in a GridView, OnRowCommand is fired for the column headers prior to the sorting event. This seems odd to me as I don't see the header row as a proper row, but I guess that's the way it is. The upshot of this is that if you add a functional button to your rows you need to specify a specific CommandName for it, so that the handler for OnRowCommand knows not only what to do, but when to (and when not to) do it:

Sub onRowCommandActioner(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

If e.CommandName = "SpecialFunction" Then
'do your special stuff
End If

End Sub

And for this you'd add the following parameter to your button field:


This means that your special stuff will only be fired for your specific command and sorting will be fired as normal. I was today however scratching my head for a little while today as it still wouldn't sort for me. This was resolved by putting my GridView into an UpdatePanel.

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!