About

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

Read more ...

Blog Search


(Supports AND)

Feed

Subscribe to the RSS feed for this blog.

See this post for info on full versus truncated feeds.

Quote

People don't do right because of the fear of God or love of him. You do the right thing because the world doesn't make sense if you don't.

Dorothy Allison



Navigation





<September 2024>
SMTWTFS
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

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 - 9/4/2024

Totals
Posts - 2655
Comments - 2677
Hits - 2,693,555

Averages
Entries/day - 0.34
Comments/entry - 1.01
Hits/day - 348

Updated every 30 minutes. Last: 2:00 PM 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] |