SIGN UP MEMBER LOGIN:    
ARTICLE

Navigating Data Grid using Client Scripting

Posted by Ezhilan Muniswaran Articles | WebForms Controls November 10, 2005
It's very common practice to use the Data Grid to present data over web pages. Though the Data Grid is provides the richer UI presentation if not coded and handled effectively it can have great performance issues. This article gives the code sample for navigating the data grid using the Client Scripting (thereby can be used for lots of validation before doing the Post Back to the server) and minimizing the round trip to server
Reader Level:

Introduction

Data Grids are gaining very popular in presenting data over the browser. If not properly coded using the datagrids can cause serious performance issue. This article is presented with some sample code for navigating through the ASP Data Grid and Infragistics Web Grid using the Java Script. By following this code sample it can be modified to accomplish lots of validation as well as setting some dynamic properties for the grid on the client side itself. And there by we can achieve lots of functionality achieved through client scripting and make improve the performance of the web page

Code Sample for Navigating ASP DataGrid

function NavigatingDataGrid(oDataGrid)

{

for(j = 0; j < oDataGrid.childNodes.length; j++)

{

var tBody = oDataGrid.childNodes(j);

for(k=0;k < tBody.childNodes.length-1; k++)

{

var tr = tBody.childNodes(k);

for(l=0; l < tr.childNodes.length-1; l++)

{

td = tr.childNodes(l);

//loop through the column and do the validation as required

//if there is any control inside the Column access the control

//by gettting the childNodes of the column

}

}

}

}

 

The cells in the data grid is accessed by getting the childNodes of the Table object that is rendered on the browser.

The first childNode gives the TBody object. The childNode of the TBody gives the Row of the table. The childNodes

of the Row gives the column in that row.If there are any controls inside the Cell it can be accessed through it's

childNodes. Once we are able to navigate the grid using the client script we can do lots of business validations like

summing the value in some column or checking for value in relevant to value on other column or rows etc. This initial

validation will help improve the performance of the Web page by minimizing the trip to the server as well as ensuring

valid data.

Code Sample for Navigating Infragistics Web Grid

(Adding Rows in Infragistics Web Grid when the data is entered on the last row)

function AfterCellUpdate(gridName,cellId)

{

var row = igtbl_getRowById(cellId);

var grid = igtbl_getGridById(gridName);

var band = row.Band

var col = igtbl_getColumnById(cellId);

if (band.Key == 'Parent')

{

if (row.getIndex() == grid.Rows.length-1)

{

igtbl_addNew(gridName,0);

var lastrow = row.getNextRow;

igtbl_addNew(gridName,1);

}

}

if (band.Key == 'Child')

{

if (row.getIndex() == row.ParentRow.Rows.length-1)

{

igtbl_addNew(gridName,1);

}

}

}

(Detecting the KeyStroke on the client Script)

function KeyDown(gn,cellId,KeyStroke)

{

if (KeyStroke == 46)

{

var row = igtbl_getRowById(cellId);

var grid = igtbl_getGridById(gn);

var band = row.Band;

if (band.Key == 'Parent')

{

return true;

}

}

if (band.Key == 'Child')

{

if (row.getIndex() == row.ParentRow.Rows.length-1)

{

return true;

}

}

}

 

(Dynamically changing the dropdown value for a cell based on the value on selected on other rows)

function BeforeEnterEdit(tableName,cellName)

{

var col = igtbl_getColumnById(cellName);

var cell = igtbl_getCellById(cellName);

var grid = igtbl_getGridById('WGA');

var row = igtbl_getRowById(cellName);

var rowno = row.getIndex();

if ((col != null) && (col.Key == 'StateDefinitionID'))

{

var blnAddKey;

var cell1;

var i; var j = 0; var row1;

var index = col.Index;

var vlist = new Array();

var vlistold = new Array();

var vkey = new Array();

for (var key in LookUp)

{

vlistold[j] = new Array (key,LookUp[key]['Name']);

j++;

}

vlistold[j] = new Array ('0',' ');

j = 0;

col.ValueList = vlistold;

for (i=0;i<grid.Rows.length;i++)

{

row1 = grid.Rows.getRow(i);

cell1 = row1.getCell(index);

if ((cell1.getValue() != 0) && (rowno != row1.getIndex()))

{

vkey[j] = cell1.getValue();

j++;

}

}

j = 0;

for (var key in LookUp)

{

blnAddKey = true;

for (var l = 0;l < vkey.length;l++)

{

if (key == vkey[l])

{

blnAddKey = false;

break;

}

}

if (blnAddKey == true)

{

vlist[j] = new Array(key,LookUp[key]['Name']);

j++;

}

}

vlist[j] = new Array ('0',' ');

col.ValueList = vlist

}

 

Code Explanation

For the infragistics client events the events can be set at design time on client side script or can be defined dynamically too. Either way once the client side events for the above functions (the events name will be same as the function name) the client events will be associated with the grid.

The first example shows how to add additional rows on the grid, when the data is entered on the last row of the grid. The band will give the level of  hierarchy of the row in the grid.Depending on the level the row new row can be added. Once the row is added the value for (ex:ID can be set to -1) and on the server side processing consider that all the row with ID -1 are new rows). This way to add the row the server trip can be avoided and make the page to perform faster.

The second example shows how to trap the keydown event on the grid. In the example we are trapping the delete key down event and returning true. This will cancel the deletion of the row. The business logic can be added here to prevent the user from deleting the row without making the trip to the server.

The last example is the unique reruirement I had in my project where as the dropdown value in one of the column can't be selected twice. This code will remove the selected value(s) from the drop down and before the column enters into the edit mode set the drop down value accordingly. This code can be modified to make the drop down dynamic as per the business requirement.The Array LookUp used in the function should have been defined in the page.

Conclusion

Hope this sample code would be useful for developers who are want to navigate the datagrids using client scripting.

Login to add your contents and source code to this article
share this article :
post comment
 

great article. very helpful www.shopperixmall.com

Posted by san batra Feb 14, 2008

Yaa Really True

Posted by Saroj Jul 24, 2006

Hello,

That was a great code snippet on making datagrid accessible on client side. Would b real good help for validation.

 

Thanks

Shashank

Posted by Shashank Kadge Nov 14, 2005
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor