title: How to Sort on a GridView using ObjectDataSource with TemplateFields
body: <h2>Background:</h2>
  
  <p>I am working with a GridView and an ObjectDataSource.  I am implementing Paging and Sorting.</p>
  
  <p>On the ObjectDataSource:</p>
  
  <pre><code>        objectDataSource.TypeName = value;
          objectDataSource.SelectMethod = "Select";
          objectDataSource.SelectCountMethod = "SelectCount";
          objectDataSource.SortParameterName = "sortExpression";
          objectDataSource.EnablePaging = true;
  </code></pre>
  
  <p>On the GridView:</p>
  
  <pre><code>        gridView.AllowPaging = true;
          gridView.AllowSorting = true;
          gridView.DataSource = objectDataSource;
  </code></pre>
  
  <p>To get paging and sorting to work, I set "EnableSortingAndPagingCallbacks" to True.  Before, I was getting a "System.Web.HttpException: The GridView fired event Sorting which wasn't handled." and this fixes it.</p>
  
  <p>If I use only BoundFields in my GridView, this is great and works fine.</p>
  
  <p>However, if I used TemplateFields, I get a "NotSupportedException: Callbacks are not supported on TemplateField because some controls cannot update properly in a callback.  Turn callbacks off on GridView."</p>
  
  <p>Which, makes sense.  I just need to know how to make sorting work, without using EnableSortingAndPagingCallbacks.</p>
  
  <p><strong>If EnableSortingAndPagingCallbacks = True:</strong></p>
  
  <ul>
  <li>Paging Works</li>
  <li>Sorting Works</li>
  <li>BoundFields Work</li>
  <li>TemplateFields do <strong><em>Not</em></strong> Work</li>
  </ul>
  
  <p><strong>If EnableSortingAndPagingCallbacks = False:</strong></p>
  
  <ul>
  <li>Paging Works</li>
  <li>Sorting does <strong><em>Not</em></strong> Work</li>
  <li>BoundFields Work</li>
  <li>TemplateFields Work</li>
  </ul>
  
  <hr>
  
  <h2><strong>My Question:</strong></h2>
  
  <p>How do I go about getting Paging, Sorting, and TemplateFields to work, all at the same time?</p>
  
  <hr>
  
  <p><em>Clarification on the implementation:</em></p>
  
  <p>Using an ObjectDataSource with a GridView requires implementing a method called Select that provides a sort expression, the number of rows to return, and the start row:</p>
  
  <pre><code>    public IEnumerable&lt;CountyAndStateGridRow&gt; Select(string sortExpression, int maximumRows, int startRowIndex)
      {
          string oql = "select County order by {" + sortExpression + "}" ;
  
          var counties = QueryProvider.ExecuteQuery(oql).Cast&lt;County&gt;();
  
          var page = counties.Skip(startRowIndex).Take(maximumRows);
  
          var rows = page.Select(
              county =&gt; new CountyAndStateGridRow
              {
                  CountyName = county.Name,
                  StateName = county.State.Name,
              });
  
          return rows;
      }
  </code></pre>
  
  <p>The specific SortExpression is defined in the aspx/ascx:</p>
  
  <pre><code>&lt;Columns&gt;
         &lt;asp:BoundField HeaderText="County Name" DataField="CountyName" SortExpression="Name" /&gt;
         &lt;asp:BoundField HeaderText="State Name" DataField="StateName" SortExpression="State.Name" /&gt;
  &lt;/Columns&gt;
  </code></pre>
  
  <p>This is <em>supposed to</em> be passed in and call the Select method on the ObjectDataSource when the column is clicked, but it does not seem to work  if EnableSortingAndPagingCallbacks = true, and instead I get the exception about the Sorting event not being defined.</p>