Wednesday, January 19, 2011 #

Client Sorting on a GridView Part B

I forgot this:

You can run this on $().ready(function() {

    SetHeaderSortLinks('aGrid', 'Get()');

});

 

 

In the global js include.

function SetHeaderSortLinks(parent, functionToCall) {

    //Clear existing links

    $("#" + parent + " th").unbind("onclick");

 

    $("#" + parent + " th").bind("click", function () {

        var sortOn = $(this)[0].sort;

 

        if (currentSortField == sortOn)

            currentSortAsc = !currentSortAsc;

 

        currentSortField = sortOn;

 

        eval(functionToCall);

    });

 

}

 

I'll probably add some css to change the cursor on mouseover based on the attribute (probably add an additional class in the function above)…

Posted On Wednesday, January 19, 2011 7:42 PM | Feedback (0)

ASP.Net GridView, Client Binding, and Sorting

 

I read a blog post a few months back about client binding your Asp.Net GridView. It was Called Bind Gridview using Jquery. I took it to heart and now I'm using it, except it doesn't handle client sorting (paging is another story). Granted this has some room for improvement and my js isn't the best…

 

Base Page method with the grid view:

 

            protected void SetHeaderAttribs(GridView grid)

            {

                  for (int i = 0; i < grid.Columns.Count; i++)

                  {

                        TableCell cell = grid.HeaderRow.Cells[i];

                        DataControlField field = grid.Columns[i];

 

                        if (!string.IsNullOrEmpty(field.SortExpression))

                        {

                              cell.Attributes.Add("sort", field.SortExpression);

                        }

                  }

            }

 

 

On the Aspx page:

 

                  <asp:GridView ID="aGrid" runat="server" AutoGenerateColumns="false" AllowPaging="false" UseAccessibleHeader="true" ShowHeaderWhenEmpty="True">

                        <Columns>

                              <asp:TemplateField HeaderText="Milestone" SortExpression="Name">

                                    <ItemTemplate>

<asp:HyperLink ID="lnkMilestoneId" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='#' onclick='<%# "Edit(" + Eval("Id") + "); return false;" %>'/>

                                    </ItemTemplate>

                              </asp:TemplateField>

 

                              <asp:BoundField DataField="DateStart" HeaderText="Start Date" SortExpression="DateStart"/>

                              <asp:BoundField DataField="DateEnd" HeaderText="End Date" SortExpression="DateEnd" />

 

 

 

In my global js include file:

 

function SortJSON(data) {

 

      if ("" == currentSortField)

            return;

 

      data.d.sort(sort_by(currentSortField, currentSortAsc, function (a) { if (null != a) return a.toUpperCase(); else return null; }));

}

 

var sort_by = function (field, reverse, primer) {

      reverse = (reverse) ? -1 : 1;

 

      return function (a, b) {

 

 

        //Note my json is an IList<X>, and the property can be something like "User.Name"

            a = eval("a." + currentSortField); //a[field];

            b = eval("b." + currentSortField); //b[field];

 

            if (typeof (primer) != 'undefined') {

                  a = primer(a);

                  b = primer(b);

            }

 

            if (a < b) return reverse * -1;

            if (a > b) return reverse * 1;

            return 0;

      }

}

 

In my ajax call:

 

function Bind (data) {

 

    //This removes older results

      $("#aGrid .dataItem").remove();

 

      //Sorting call

      SortJSON(data);

 

    //Add the columns (give it a class dataItem so I can remove them cleanly with jquery above.

      for (var i = 0; i < data.d.length; i++) {

            $("#aGrid").append(

                  "<tr class='" + OddEven(i) + " dataItem'>"

 

                  + "<td><a href='#' onclick='Edit (" + data.d[i].Id + ");return false;'>" + data.d[i].Name + "</a></td>"

                  + "<td>" + data.d[i].StrDateStart + "</td>"

                  + "<td>" + data.d[i].StrDateEnd + "</td>"

 

                  + "</tr>"

            );

      }

}

 

 

Posted On Wednesday, January 19, 2011 5:49 PM | Feedback (0)

Domain Hosting

 

After hosting my domains with Godaddy for a few years, I was tired of the extra charge for private registration. I found that Hover offers it free and a few months back, I switched one of my domains. Wow! Godaddy really made that hard: When I authorized the transfer, Godaddy immediately stopped resolving DNS lookups even before domain propagation completed—this resulted in any email or web requests failing because their server did not return those server ips. When I called them, they either were clueless and did not know what they were doing or deliberately misled me to believe they had not done that and were not at fault. To make a long story longer, becareful leaving Godaddy—setup a new Name Server and change it before you approve the domain transfer.

 

Hope this saves somebody else…

Posted On Wednesday, January 19, 2011 5:16 PM | Feedback (0)

Copyright © Josh Tenenbaum

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski