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

If you don't know whether you're a bad manager, then you're a bad manager. It's the default state, the start-state, for managers everywhere. So just assume you're bad, and start working to get better at it.

Steve Yegge



Navigation





<September 2014>
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

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  

Contact

Email me

Blog Statistics

Dates
First entry - 6/27/2003
Most recent entry - 7/23/2014

Totals
Posts - 2304
Comments - 2495
Hits - 1,659,070

Averages
Entries/day - 0.56
Comments/entry - 1.08
Hits/day - 406

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