Tuesday, February 3, 2015

Natural Sorting Using Linq For DataTable C#.


public partial class MasterDetailLn_LoanIssue : System.Web.UI.Page
{
private void function()
{
DataTable dtLn_LoanPaid = Ln_LoanPaidService.GetLn_LoanByDepartment();
   DataTable dtsorted=new DataTable();
        if(dtLn_LoanPaid!=null &&dtLn_LoanPaid.Rows.Count>0)
         dtsorted= dtLn_LoanPaid.Select().OrderBy(O=>O.Field<String>("PFNo"),new NaturalSortComparer<string>()).CopyToDataTable();
    }
}
public class NaturalSortComparer<T> : IComparer<string>, IDisposable
{
    private bool isAscending;

    public NaturalSortComparer(bool inAscendingOrder = true)
    {
        this.isAscending = inAscendingOrder;
    }

    #region IComparer<string> Members

    public int Compare(string x, string y)
    {
        throw new NotImplementedException();
    }

    #endregion

    #region IComparer<string> Members

    int IComparer<string>.Compare(string x, string y)
    {
        if (x == y)
            return 0;

        string[] x1, y1;

        if (!table.TryGetValue(x, out x1))
        {
            x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
            table.Add(x, x1);
        }

        if (!table.TryGetValue(y, out y1))
        {
            y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
            table.Add(y, y1);
        }

        int returnVal;

        for (int i = 0; i < x1.Length && i < y1.Length; i++)
        {
            if (x1[i] != y1[i])
            {
                returnVal = PartCompare(x1[i], y1[i]);
                return isAscending ? returnVal : -returnVal;
            }
        }

        if (y1.Length > x1.Length)
        {
            returnVal = 1;
        }
        else if (x1.Length > y1.Length)
        {
            returnVal = -1;
        }
        else
        {
            returnVal = 0;
        }

        return isAscending ? returnVal : -returnVal;
    }

    private static int PartCompare(string left, string right)
    {
        int x, y;
        if (!int.TryParse(left, out x))
            return left.CompareTo(right);

        if (!int.TryParse(right, out y))
            return left.CompareTo(right);

        return x.CompareTo(y);
    }

    #endregion

    private Dictionary<string, string[]> table = new Dictionary<string, string[]>();

    public void Dispose()
    {
        table.Clear();
        table = null;
    }
}

Thursday, January 15, 2015

Finding PostBack Control from ClientSide JavaScript in ASP .net.

We can Find the Control Creating PostBack in ASP .net webform from ClientSide Script using following Code and Also can Capture the end of Asynchronous postback event in case of update panel  where generally focus is lost after asynchronous post back.

 $(function () {

 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
         if (sender._postBackSettings.sourceElement.id == '<%=ddlLedger.ClientID %>')

                       //Your Control creating postback
                      $("#ctl00_ContentPlaceHolder1_ddlSubLedger_chosen .chosen-single").focus();
         else {
                  //do your thing
                }

            });        
});


Hence on endrequest we can get the postback control on completion of post back and capture the after post back event to do what needed to like may be a focus to next control which is usually lost in case we are using update panel or in case of chosen dropdown.