GridView Sorting Event Handler Called Twice on one OnSorting Event

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
Fields and stuff

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.


Tags: , , , , ,

17 Responses to “GridView Sorting Event Handler Called Twice on one OnSorting Event”

  1. roleki Says:

    Argh, this was driving me bonkers. Thanks for pointing that out; I was under the impression that you *had* to callout the Handler in markup, but sadly, this is not the case.

    I say sadly because… apparently everything I’ve ever done with a Handler has executed twice & if I hadn’t got the bright idea to try an OnSortCommand handler, I’d never have noticed it.

    • baraholka1 Says:


      Very glad to assist a fellow traveller in the Journey Of Pain (actually I might change my Blog title to that…)

      Please send money.

      Best Regards,


  2. Barun Says:

    Thanks… It saves me from a long pain also. I had wasted 6 7 hrs. on this sily problem.

    • baraholka1 Says:


      May your Journeys of Pain become ever less.
      For instant relief rub Wax Tadpole over affected parts.
      Thanks for dropping by and glad to assist.
      You have given meaning to my Grid Sort misery.

      – Barra

  3. Sam Says:

    This problem actually made my eyes bleed a little.
    Thank you so so so so so so soooo SO much for stopping that.

    • baraholka1 Says:


      When suffering troublesome pain, rub Wax Tadpole over affected areas.
      Thanks for dropping by and v. glad to be of assistance.

      – Barra

  4. DNSA Edwards Says:

    Thanks so much for this – I went back in time trying to figure this out. It was seriously twisting my melons. Bring back “classic” ASP – you knew where you stood man!

    • baraholka1 Says:

      There’s another good blog title: ‘Twisted Melons’ !
      Glad I could provide the Panadol for your Journey Of Pain 🙂

  5. Ophilia Says:

    Thank you, this was driving me crazy!!

    • baraholka1 Says:

      Dear Ophilia,

      Great to hear from lovers of the letter O.
      Enjoy your newly regained sanity.
      Am I normal ?



  6. Jim Prucha Says:

    Is this relevant with C#? I am having this issue with my gridview when clicking a sort header with using c# for my code behind pages.



    • baraholka1 Says:


      May I call you James ?

      It is indeed possible to have two handlers applied to the one event (or the same handler twice) in C#. Look for gridview.sort += youronsorthandler in code behind and see if you also have a handler defined in markup.

      Yrs From The Inverted Lotus Position,


      • Jim Prucha Says:

        Hi Barra,

        I do not have double event handlers. I was able to do further searching and find out the when you use an asp:Image for your sort headers that the asp:Image and the javascript that is injected by .net it gets posted twice. To get around this I actually made the HeaderText property an image link. So my HeaderText looks something like this — HeaderText=”” and it works like a charm.



      • Jim Prucha Says:

        basically the HeaderText is just an img tag referencing my sort image.

      • baraholka1 Says:

        Nice work Jim,

        That’s sure to assist someone on the same Journey Of Pain.
        Thanks for dropping by.

        I should also mention that Jim has now roared to the top of BTWT Commenters count with a superlative grand total of three comments. He is rewarded for his toil by this message here stated.

        – Barra

  7. extazium Says:

    Thanks a ton. I mean it. Stupid VB 😦

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: