Dynamic Nested ASP.Net GridView using AJAX, Webservices and jQuery

Introduction

Eugene Niemand
Eugene Niemand’s Blog
4 min readAug 25, 2010

--

I haven’t blogged for a while, I have been really swamped at work.

Anyway last week I after using jQuery for a while (for those who don’t know jQuery is awesome, take the time to try it) I had a brilliant idea. After numerous previous attempts at building nested ASP.Net GridViews I thought hang on a minute, once a gridview has been rendered it is normal HTML. Thus its a table with rows(<tr>) and columns(<td>), so all I need to do is somehow get a empty row after every row to act as a place holder(<div></div>) for the nested grid.

RowDataBound

I did a couple of Google searches and found weird and wonderful ways to implement the empty rows but most of them were far to complex for what I needed. Then I found a post (sorry I cant remember the source) with the following code for the RowDataBound event:

Ok so the empty row/div has been added. Once rendered if found that you can see these gaps between every row and it didn’t look very nice. So I added a CSS attribute like this:

There we go they are now hidden. Now onto the next step, getting another GridView into that empty div. Firstly we need to somehow trigger something to say load data and put the response in the div, thus it was necessary to add a couple more attributes to the rows and the divs.

The Div Attributes

The first line is to give every empty div a unique ID so we can reference it from jQuery. Next I gave it a class name, this is just for display purposes you don’t need this. Lastly I gave it a custom attribute ParentDiv with the ClientID of it parent row, this will be explained a little later.

The GridViewDataRow Attributes

The first line is a class name, this is just for display purposes you don’t need this, next I added a CSS cursor style to show the little hand, again you don’t need this. The next one is where the fun starts, I’ll come back to explain that.

WebService

In order for us to get a response I used a webservice that has been exposed to the Javascript by adding a Service reference to the ScriptManager (I think this functionality is only available in .Net 3.5+), you have to add the following line to your class descriptors <System.Web.Script.Services.ScriptService()> for this to work.

Inside GetRAR_AM I create a new GridView and bind it and return the HTML, but I’m not going into those details.

Client Side Javascript and jQuery

Below you will see the Javascript and jQuery code, you will see the function name is GetSTAM and expects 3 parameters, the function call is set-up in the last line in GridViewdataRow attributes. When this function is called it will call the webservice asynchronously. There are 3 extra parameters onComplete, onError and Div. The first to is self explanatory and they will fire on those events, the Div is passed along as a context/id so the onComplete and onError can use that div to update the appropriate div with the response of the webservice. The args parameter is the response from the webservice.

--

--

Eugene Niemand
Eugene Niemand’s Blog

Data Solution Architect at ASOS - I have a passion for Test Driven Development, Agile Methodologies, Continuous Integration and Delivery using Microsoft Azure