About

I'm Mike Pope. I live in the Seattle area. I've been a technical writer and editor for over 30 years. I'm interested in software, language, music, movies, books, motorcycles, travel, and ... well, lots of stuff.

Read more ...

Blog Search


(Supports AND)

Google Ads

Feed

Subscribe to the RSS feed for this blog.

See this post for info on full versus truncated feeds.

Quote

We should be taught not to wait for inspiration to start a thing. Action always generates inspiration. Inspiration seldom generates action.

Frank Tibolt



Navigation





<September 2023>
SMTWTFS
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

Categories

  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  
  RSS  

Contact Me

Email me

Blog Statistics

Dates
First entry - 6/27/2003
Most recent entry - 6/27/2023

Totals
Posts - 2648
Comments - 2662
Hits - 2,599,737

Averages
Entries/day - 0.36
Comments/entry - 1.01
Hits/day - 352

Updated every 30 minutes. Last: 7:34 AM Pacific


  10:50 PM

One of the things I like is how as you work with ASP.NET (or anything), your instincts develop a bit and you can figure out how to do stuff with only a few dents in your forehead where you've been banging it against the keyboard.

I have an app that uses a GridView control to display some information. The GridView control includes sorting (for free), which is bidirectional (also for free). First you set AllowSorting[1] to true, them for each column (bound or templated), you set a sort expression. This renders a LinkButton in the column header, and the grid toggles between sorting ascending and descending per your sort expression. Nice.

For various reasons, in one of my apps I have to do the bi-directional part manually. But this isn't very hard at all. First, in the GridView column, I specified a custom sort expression (that is, an expression that doesn't map directly to a database field):
<asp:TemplateField HeaderText="Date" SortExpression="datesort">
<ItemTemplate>
<asp:Label ID="labelPDate" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
In the page code, I created a DateSortDirection property, and I initialize it in the Page_Load handler.
Public Property DateSortDirection() As String
Get
Return ViewState("DateSortDirection")
End Get
Set(ByVal value As String)
ViewState("DateSortDirection") = value
End Set
End Property


Protected Sub Page_Load()
If Not Page.IsPostBack Then
Me.DateSortDirection = "ASC"
End If
End Sub
To implement bidirectional sorting, I handle the grid's Sorting handler:
Protected Sub GridView1_Sorting(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
If e.SortExpression = "datesort" Then
If Me.DateSortDirection = "ASC" Then
e.SortExpression = "YEAR DESC, MONTH DESC, DAY DESC"
Me.DateSortDirection = "DESC"
Else
e.SortExpression = "YEAR ASC, MONTH ASC, DAY ASC"
Me.DateSortDirection = "ASC"
End If
End If
End Sub
As an added touch, I wanted to display an arrow in the header to indicate which direction the sort was:



I created two little .gif files -- an up arrow and a down arrow. To display them in the header, I dynamically added an Image control (plus a space) to the appropriate column of the header, like this:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
Dim img As New Image
If Me.DateSortDirection = "ASC" Then
img.ImageUrl = "asc.gif"
Else
img.ImageUrl = "desc.gif"
End If
e.Row.Cells(0).Controls.Add(New LiteralControl(" "))
e.Row.Cells(0).Controls.Add(img)
End If
End Sub
And that was it. Works great!

[1] Per our style guide, this should be EnableSorting.

[categories]   ,

[15] |