This Is Not An Excuse, It’s a REASON.
It’s been three and a half years since I coded in VB.NET so while developing/maintaining a VB.NET app. on my current project I have been shamelessly copying cannily re-using a lot of existing code.
The event handlers in this app. all have “Handles” clauses hanging off them so I naively assumed this was required VB syntax. Thus began my latest journey in pain.
Something I Learned At Work Today
Did you know that when a GridView does not have a Data Source Control defined that it does not automatically Sort when you click on the Column Headers ? Of couse you did, Vikram Lahotia. I, however, did not as all my Grids had nice ObjectDataSources or similar.
THIS particular one, however, is bound to a DataTable stored in ViewState (N.B. I did not write this page) , so after I found out why it didn’t Sort automatically, I had to handle the OnSorting event, which I did as you might expect:
asp:GridView ID="TrevTheGridView runat="server" etc
OnSorting="TrevTheGridView_OnSorting
Fields and stuff
/asp:GridView
In addition, due to my VB.NET naivety I also did this in code-behind:
Protected Sub TrevTheGridView_Sorting(ByVal sender as Object, ByVal e as GridViewSortingArgs) Handles trevTheGridView.Sorting
‘Flip The Sort Order
‘Define a DataView on the ViewState datatable
‘Assign SortExpression to DataView
‘Rebind Trev to the DataView.ToTable
End Sub
Notice that Handles clause? It’s redundant. I repeat: It’s redundant. The combination of the Handles gridView.Sorting in code-behind and OnSorting=”sortEventHandler” in Markup meant that the Sorting event handler was called twice which caused my Sort Order to be flipped twice meaning that the GridView Sort Order never changed.
Tremendous. If I want to watch something never change I’ll go live in Delaware or hook up to Richie Benaud’s hairdresser on WebCam.
So get rid of the Handles clause if you only want the event handler firing once. Or the declaration if the Event Handler in Markup.