ASP.NET Controls – Problem sorting GridView with SqlDataSource control

by nmgomes

Let me start by saying that Microsoft don't consider this issue as a problem, as you can see here this is a “by design” behavior.

The problem is well described in the referred Connect feedback and it contains a workaround.

Although simple, the workaround requires you to always register the GridView Sorting event and make the tweak according to the current GridView settings. Well, if are like me you will forget to do it half the times needed.

So, I made a not so simple workaround that will take care of the issue for me.

I override the OnSorting method from GridView so I can handle the GridViewEventArgs instance and override its SortDirection value.

To turn this into a general solution I partially reproduce the ParseSortString method from DataTable to find out if the current SortExpression contains either the ASC or DESC keywords.

Here is the code:

public class GridView : global::System.Web.UI.WebControls.GridView{    protected override void OnSorting(GridViewSortEventArgs e)    {        if (!string.IsNullOrEmpty(this.SortExpression))        {            if (this.SortExpression.Equals(this.SortExpression))            {                bool isMultipleSortExpression;                SortDirection? sortDirection = GetSortDirection(this.SortExpression, out isMultipleSortExpression);                if (sortDirection.HasValue)                {                    // To undo bug in GridView.HandleSort(string sortExpression) and then in GridView.CreateDataSourceSelectArguments()                    e.SortDirection = SortDirection.Ascending;                }            }        }        base.OnSorting(e);    }    private SortDirection? GetSortDirection(string sortExpression, out bool isMultipleSortExpression)    {        SortDirection? sortDirection = null;        isMultipleSortExpression = false;        string[] strArray = sortExpression.Split(new char[] { ',' });        for (int i = 0; i < strArray.Length; i++)        {            string strA = strArray[i].Trim();            int length = strA.Length;            if ((length >= 5) && (string.Compare(strA, length - 4, " ASC", 0, 4, StringComparison.OrdinalIgnoreCase) == 0))            {                sortDirection = SortDirection.Ascending;            }            else if ((length >= 6) && (string.Compare(strA, length - 5, " DESC", 0, 5, StringComparison.OrdinalIgnoreCase) == 0))            {                sortDirection = SortDirection.Descending;            }            if (!sortDirection.HasValue)            {                break;            }        }        if (sortDirection.HasValue)        {            if (strArray.Length > 1)            {                isMultipleSortExpression = true;            }        }        return sortDirection;    }}

Enjoy it.

Filed in: ASP.NET | CodeProject



by nmgomes


Next October, 2 ReMIX 09 will be in Portugal.

I’ll be there.

If you are nearby, take the chance and came visit us.

Hope to see you there.

Filed in: Events | ReMIX


Typemock Racer – A “must have tool” for all ASP.NET developers

by nmgomes

A very common mistake taken by junior ASP.NET developers is forgetting that any web application running in a IIS instance will most likely run in a multithread environment.

They forgot that, unlike the dev environment which usually runs in a single thread pool, production environment usually have a working thread pool with several dozen threads.

Common mechanism like the ASP.NET Cache or the ASP.NET Session have points of concurrency.

It’s common to create singletons in web apps. The most common scenario are provider pattern implementations. Many times, apps also need to have it’s own Cache mechanism or even Session mechanism. All this scenarios may leads us to multi thread racing for some resources.

Testing those code pieces were almost impossible … and multithread bugs and deadlocks were detected late in the application developing cycle:

  • when running load tests
  • or, in the worst scenario, only in a production environment.

Those days are gone … last May Typemock added a new tool to its toolkit, the Typemock Racer.

This new tool is intended to help finding and fixing possible deadlocks by enabling us to write threaded tests.

And if you think that this is a complex task to do, you are wrong. Doing race tests is as simple as writing a unit test and then decorate it with an  attribute – [ParallelInspection].

That’s it .. you can then run the test and Racer will try to find and recreate deadlocks.

In future Racer will also check for race conditions.

Take a look at this Roy Osherove post for some snapshots and a little movie.

Filed in: ASP.NET | Tools | Unit Tests