Skip to main content

Creating Master-Detail GridView Using jQuery

Abstract:

In the year 2005 we published an article about creating master-detail GridView control. JQuery was not a first class citizen during that time and most of the work was performed by either plain old vanilla JavaScript or the server side code. With the advent of JQuery we can now create cool master-detail effects easily in less time and less code. This article explains how to spice up master-detail using JQuery.

Data Source:



For the sake of simplicity we have used collections as the data source instead of the database. The scenario revolves around Category and Product classes. The implementation below is used to populate the categories collection and return it to the user interface.



view sourceprint?

01 public class DataAccess

02 {

03 public static List GetCategories()

04 {

05 var categories = new List();

06

07 for(var i = 1; i<=10;i++)

08 {

09 var category = new Category();

10 category.Name = "Category " + i;

11 category.Description = "Description for Category " + i;

12 category.Products = new List();

13

14 for(var j=1; j<=10;j++)

15 {

16 var product = new Product() {Name = "Product " + j, Price = 10*j};

17 category.Products.Add(product);

18 }

19

20 categories.Add(category);

21 }

22

23 return categories;

24 }

25 }





The above code populates the categories collection with some dummy data and assigns ten products to each category. The DataAccess.GetCategories is triggered from the page code behind as shown in the code below:



view sourceprint?

01 public partial class MasterDetailGridViewUsingJQuery : System.Web.UI.Page

02 {

03 protected void Page_Load(object sender, EventArgs e)

04 {

05 if(!Page.IsPostBack)

06 {

07 BindData();

08 }

09 }

10

11 private void BindData()

12 {

13 gvCategories.DataSource = DataAccess.GetCategories();

14 gvCategories.DataBind();

15 }

16 }





The gvCategories is the name of the GridView control which is being populated using the categories collection returned from the DataAccess.GetCategories method.



Applying JQuery Effect on the GridView Control:



Before applying JQuery effect let's take a look at the ASPX code for the GridView control.



view sourceprint?

01

02

03

04

05

06

07 <%# Eval("Name") %>

08

09

13

14

15


16


17

18

19


20

21






When the page is loaded for the first time only category name is being displayed as shown in the screenshot below:







The JQuery implementation is shown in the code below:



view sourceprint?

01





Basically we are attaching the click function to all the elements on the page whose class name is ".categoryStyle". Inside the click event we use the JQuery siblings function to find the element with the class ".productStyle" and make it show and hide. The effect is shown below:







Conclusion:



In this article we learned how to use JQuery effects to enhance the user experience when implementing master-detail GridView scenarios.

Comments

Popular posts from this blog

Drawing Line Charts In asp.net

LineChart.cs import the following namespaces using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Collections; public class LineChart { public Bitmap Bitmapb; public string strTitle = "Default Title"; public ArrayList ArrayListchartValues = new ArrayList(); public float ftXorigin = 0, ftYorigin = 0; public float ftScaleX, ftScaleY; public float ftXdivs = 2, ftYdivs = 2; private int intWidth, intHeight; private Graphics Graphicsg; private Page Pagep; struct datapoint { public float ftx; public float fty; public bool bolvalid; } //initialize public LineChart(int intmyWidth, int intmyHeight, Page myPage) { intWidth = intmyWidth; intHeight = intmyHeight; ftScaleX = intmyWidth; ftScaleY = intmyHeight; Bitmapb = new Bitmap(intmyWidth, intmyHeight); Graphicsg = Graphics.FromImage(Bitmapb); Pagep = myPage; } pu

AJAX

ASP.NET AJAX From Wikipedia, the free encyclopedia Jump to: navigation, search ASP.NET AJAX is a set of extensions to ASP.NET developed by Microsoft for implementing Ajax functionality. It is released under the Microsoft Public License. Contents [hide] • 1 Road Map • 2 Browser support • 3 ASP.NET AJAX Suite • 4 Microsoft Ajax Library • 5 The UpdatePanel Control • 6 Script Controls and Extenders • 7 Web-services and JSON • 8 See also • 9 References • 10 External links [edit] Road Map In its early development stages, ASP.NET AJAX was given the code name Atlas. Initial releases were made public to the development community in the form of CTPs (Community Technical Previews). The production version, ASP.NET AJAX 1.0 RTM; was released on January 23, 2007[1][2] as an extension to ASP.NET 2.0. Important release milestones for ASP.NET AJAX are as follows: • CTPs - October, December 2005, January, March, April, June, July 2006 • Beta - October, November 2006 • Release Candidate (RC) - December 2