jQuery Flexigrid Using C# 3.0 (.NET 3.5) & LINQ
23 Jul 2008When I developed my first ASP.NET MVC application, I was a little disappointed with my options for a rich grid. I initially used the grid that is part of the MVCContrib project, but it is pretty simple and there arenβt many features out of the box.
I was very pleased with the code of my MVC application, but the presentation was so 1990βs. With all of this new technology I thought the presentation deserved something snappy. That is when I found Flexigrid.
As I mentioned in my last blog entry, I started to use jQuery. To my joy, Flexigrid is a jQuery plugin! Flexigrid uses jQuery to asynchronously populate the contents of the grid using either XML or JSON input.
The following is an example of what the grid looks like. It contains features to sort, page, search, move columns, resize, etcβ¦
The User Interface portion was pretty straightforward to put together. You just need to define your columns, the data source, and some additional parameters (such as: search terms, size, etcβ¦).
<pre>
</pre><table></table>
I wanted to use this opportunity to try out some new features of .NET 3.5, so I wanted to incorporate LINQ and JSON serialization.
To do this, I needed to setup some classes that the Flexigrid will recognize once serialized. Here is what I came up with.
<div><div><pre> 1: public class FlexigridViewData</pre><pre> 2: {</pre><pre> 3: public int page;</pre><pre> 4: public int total;</pre><pre> 5: public List
<div><div><pre> 1: public void Page_Load()</pre><pre> 2: {</pre><pre> 3: Response.Clear();</pre><pre> 4: Response.ContentType = "text/x-json";</pre><pre> 5: Response.Write(GetPagedContent());</pre><pre> 6: Response.Flush();</pre><pre> 7: Response.End();</pre><pre> 8: }</pre><pre> 9:Β </pre><pre> 10: private string GetPagedContent()</pre><pre> 11: {</pre><pre> 12: var pageIndex = Convert.ToInt32(Request.Params["page"]);</pre><pre> 13: var itemsPerPage = Convert.ToInt32(Request.Params["rp"]);</pre><pre> 14: var sortName = Request.Params["sortname"];</pre><pre> 15: var sortOrder = Request.Params["sortorder"];</pre><pre> 16: var query = Request.Params["query"];</pre><pre> 17:Β </pre><pre> 18: IEnumerable
<div><div><pre> 1: public static class JsonHelper</pre><pre> 2: {</pre><pre> 3: public static string ToJson(this object obj)</pre><pre> 4: {</pre><pre> 5: var serializer = new JavaScriptSerializer();</pre><pre> 6:Β </pre><pre> 7: return serializer.Serialize( obj );</pre><pre> 8: }</pre><pre> 9:Β </pre><pre> 10: public static string ToJson(this object obj, int recursionDepth)</pre><pre> 11: {</pre><pre> 12: var serializer = new JavaScriptSerializer();</pre><pre> 13:Β </pre><pre> 14: serializer.RecursionLimit = recursionDepth;</pre><pre> 15:Β </pre><pre> 16: return serializer.Serialize( obj );</pre><pre> 17: }</pre><pre> 18: }</pre></div></div><div><div><pre> 1: public static T GetPropertyValue
I would prefer if the developer of the product had a better system of tracking bugs and maintaining a forum, but in the meantime what is setup is sufficient.