Oct2009
9

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

May2009
22

ASP.NET – Converting C# String to JSON String

by nmgomes

Most web applications display messages to users. Displaying messages is the most effective way to inform user about errors and warnings or to simply display info or success status.

I also believe that most of those web applications renders user messages using HTML elements such as Div, Span or Label.

The others, mainly because their complex layout or business rules, have to render those messages using a different approach: they render each display message as a Javascript function call where the message itself is a function parameter.  Such call could be simply like this:

string.Format("top.addMessage(\"{0}\");", message)

Those of you that already faced this problem won’t learn nothing new but I hope to alert the others and avoid them to face a problem that usually is only detected in advanced develop stage or even production environment.

Problem

The problem turns visible when the display message contains some special characters that prevent the function call to execute and trigger javascript errors.

Typically the characters are “ and ‘ but there are more.

Well, that is not completely correct, only the character “ is problematic, since he is the JSON string delimiter character, but, javascript engines also accept the character ‘ as a delimiter. The best way to avoid problems with ‘ is to ensure that the correct delimiter is used.

By now, you should be thinking that your applications are immune to this problem. Tell me, do you use string Resources? Most of us use, it’s a best practice and a smart choice. Also tell me, do you manage production string Resources? Most of us don’t. In fact, the application owner or sponsor can change it completely outside of your control scope.

String Resources are one of the major vectors of this problem but you can get this problem simply by trying to display an exception message:

string.Format("top.addMessage(\"{0}\");", exception.ToString())

Solution

What we are needing is an JScript.Encode method or similar that encode a C# string in such a way that it becomes a JSON encoded string.

Such method is not available on NetFX but, fortunately, Rick Strahl made a great post about this problem and made available a method that fits perfectly our needs.

Now that you know about it … use it.

Filed in: ASP.NET

May2009
18

ASP.NET unit testing – ASP.NET Bundle by Typemock

by nmgomes

Unit Testing ASP.NET? ASP.NET unit testing has never been this easy.

Typemock is launching a new product for ASP.NET developers – the ASP.NET Bundle - and for the launch will be giving out FREE licenses to bloggers and their readers.

The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both Typemock Isolator, a unit test tool and Ivonna, the Isolator add-on for ASP.NET unit testing, for a bargain price.

Typemock Isolator is a leading .NET unit testing tool (C# and VB.NET) for many ‘hard to test’ technologies such as SharePoint, ASP.NET, MVC, WCF, WPF, Silverlight and more. Note that for unit testing Silverlight there is an open source Isolator add-on called SilverUnit.

The first 60 bloggers who will blog this text in their blog and tell us about it, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET dedicated blog, you'll get a license automatically (even if more than 60 submit) during the first week of this announcement.

Also 8 bloggers will get an additional 2 licenses (each) to give away to their readers / friends.

Go ahead, click the following link for more information on how to get your free license.

Filed in: ASP.NET | Tools