How to: use Infragistics UltraWebGrid RowEditTemplate using Javascript in ASP.NET 2.0 (C#)

Infragistics UltraWebGrid is an important tool in ‘NetAdvantage for ASp.NET’ Suite. UltraWebGrid has built in AJAX functionality, along with enhanced performance and improved GUI.

 

In this article we’ll learn how to use UltraWebGrid’s RowEditTemplate using JavaScript. But I’ll be showing the necessary code to be modified.

 

You have to register the assembly in the page you wish to use UltraWebGrid using ‘Register’ Tag

 

Then you have to add the UltraWebGrid to your page

 

<igtbl:ultrawebgrid id=”UWGItems” runat=”server” width=”100%” height=”150px” browser=”Xml” oninitializelayout=”UWGItems_InitializeLayout”></igtbl:ultrawebgrid>

 

In your codefile define the UWGItems_InitializeLayout method

 

protected void UWGItems_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs e)

{

// The InitializeLayout event is called when the grid binds to data. Many of these

// settings can be set at design time, rather then run time, via the property window.

// For this sample, these properties are set in code so that the settings are more easily visible.

 

// Turn on addnew, update and delete for the grid

e.Layout.AllowAddNewDefault = AllowAddNew.Yes;

e.Layout.AllowUpdateDefault = AllowUpdate.Yes;

e.Layout.AllowDeleteDefault = AllowDelete.Yes;

 

//Add the AddNew button

e.Layout.AddNewBox.Hidden = false;

e.Layout.Bands[0].AddButtonCaption = “Add New”;

e.Layout.Bands[0].AddButtonToolTipText = “Click here to Add New.”;

 

 

//// The Primary key field in most cases is an autogenerated number and will not be updated or set by the user. This

//// will prevent user action to modify the value

e.Layout.Bands[0].Columns.FromKey(“Sr”).AllowUpdate = AllowUpdate.No;

 

//// The DataKeyField of the Band should be set to PrimaryKey of the underlying table. To set the DataK

e.Layout.Bands[0].DataKeyField = “Sr”;

 

 

}

 

In your page’s Page_load event you can bind the grid to any data source e.g.

 

this.UWGItems.DataSource = this.TempDt;

this.UWGItems.DataBind();

 

Assuming that we’ve four columns in TempDT:

1) Sr. Serial Number

2) ItemName

3) ItemCode

4) Amount

 

These columns will be shown in your UltraWebGrid along with the * with each row for row editing and an ‘Add New’ button at the bottom of the grid. By clicking on ‘*’ or ‘Add New’ Button will open up the RowEditTemplate which we’ll be using later in this article.

 

Define the RowEditTemplate for your UltraWebGrid by adding the following lines of code to your Grid HTML

 

<bands>

<igtbl:ultragridband>

<rowedittemplate>

<table border=”0″ width=”100%”>

<tbody><tr>

<td colspan=”2″ align=”center”>Item Description</td>

</tr>

<tr>

<td align=”right”>Item Name:</td>

<td align=”left”><input id=”tmplItemName” size=”50″

 

columnkey=”ItemName” type=”text”></td>

</tr>

<tr>

<td align=”right”>Code:</td>

<td align=”left”><input id=”tmplItemCode” disabled=”disabled”

 

value=”821311″ size=”8″ type=”text”></td>

</tr>

<tr>

<td align=”right”>Amount:</td>

<td align=”left”><input id=”tmplItemAmount” size=”18″

 

columnkey=”Amount” type=”text”></td>

</tr>

<tr>

<td>

</td>

<td align=”left”>

<input id=”igtbl_reOkBtn”

 

onclick=”ValidateTemplate();igtbl_gRowEditButtonClick(event);” style=”width: 67px;” value=”OK”

 

type=”button”>

<input id=”igtbl_reCancelBtn”

 

onclick=”igtbl_gRowEditButtonClick(event);” style=”width: 67px;” value=”Cancel”

 

type=”button”>

</td>

</tr>

</tbody></table>

</rowedittemplate>

</igtbl:ultragridband>

</bands>

 

You may wish to validate the boxes on OK button click, you can define your ValidateTemplate() function and put necessary code according to your requirements

 

The function call igtbl_gRowEditButtonClick(event); is UltraWebGrid’s built in call that will handle the ‘OK’ or ‘Cancel’ button click

 

In between your <displaylayout></displaylayout> tags, you’ve to add a line which describes some client side functions

 

<clientsideevents dblclickhandler=”DblClick” beforerowtemplateclosehandler=”BeforeRowTemplateClose”

 

afterrowtemplateclosehandler=”AfterRowTemplateClose”></clientsideevents>

 

What happens when a user double clicks on any row or item, our function will just return 1 without doing anything means that there would be no operation in that case

 

// called when a cell, row label, or column header is double clicked

function DblClick(tableName, itemName)

{

return 1;

}

 

Our Purpose is to handle the RowEditTemplate using javascript, This following function will be called just before the RowEditTemplate is being closed.

We’ll be showing the next serial id and code using javascript, rest of the columns will be mapped automatically

 

function BeforeRowTemplateClose(gridName,rowId,bSaveChanges)

{

if(bSaveChanges)

{

var ActiveRow = igtbl_getActiveRow(gridName);

var oGrid = igtbl_getGridById(gridName);

var MaxCount=0;

var arr = rowId.split(“_”);

var ActualRowID = arr[arr.length-1];

var RowsCounter = oGrid.Rows.length;

 

for(c = 0; c < RowsCounter; c++)

{

var row = oGrid.Rows.getRow(c);

var CounterCell = row.getCell(0);

if(CounterCell.getValue()!=null)

MaxCount=CounterCell.getValue();

else if(CounterCell.getValue()==null && ActualRowID!=c)

oGrid.Rows.remove(c);

}

if(ActiveRow != null)

{

// Obtain the Cell object for Sr Column and Update the Cell value

if(ActiveRow.getCell(0).getValue()==null)

ActiveRow.getCell(0).setValue(MaxCount+1);

// Obtain the Cell object for Code Column and Update the Cell value

ActiveRow.getCell(2).setValue(“821311”);

 

}

}

}

 

if(bSaveChanges):

OK button of RowEditTemplate is clicked

 

var ActiveRow = igtbl_getActiveRow(gridName);

Get the active row of grid by specifying grid name

 

var arr = rowId.split(“_”);

var ActualRowID = arr[arr.length-1];

Split the row ID and get the actual rowID

 

for(c = 0; c < RowsCounter; c++)

{

var row = oGrid.Rows.getRow(c);

var CounterCell = row.getCell(0);

if(CounterCell.getValue()!=null)

MaxCount=CounterCell.getValue();

else if(CounterCell.getValue()==null && ActualRowID!=c)

oGrid.Rows.remove(c);

}

Loop through the grid till the RowsCounter, traverse through each row go till the last row, get the SerialNumber value and assign it to MaxCount, if the CounterCell’s value is null then delete that un-necessary row

 

if(ActiveRow != null)

{

// Obtain the Cell object for Sr Column and Update the Cell value

if(ActiveRow.getCell(0).getValue()==null)

ActiveRow.getCell(0).setValue(MaxCount+1);

// Obtain the Cell object for Code Column and Update the Cell value

ActiveRow.getCell(2).setValue(“821311″);

 

}

Check if the ActiveRow is not null and active row’s cell 0 has null value then place the new SerialNumber by adding 1 to MaxCount we already calculated.

We may wish to display some static code to the ItemCode column, for that purpose we’ve to place that value to active row’s cell no. 2 which is the ItemCode column.

 

Now you may wish to display the total of amounts entered in the Amount column, for this you will be writing javascript

 

function ‘AfterRowTemplateClose’

function AfterRowTemplateClose(tableName,itemName)

{

var oGrid = igtbl_getGridById(tableName);

var RowsCounter = oGrid.Rows.length;

//loop through grid values to calculate the total

var Amount=0;

for(c = 0; c < RowsCounter; c++)

{

var row = oGrid.Rows.getRow(c);

if(row.getCell(1).getValue()!=null)

{

var AmountCellVal = row.getCell(3).getValue();

Amount += parseInt(GetAmount(AmountCellVal));

}

}

// Obtain the txtTotalAmount textbox and update its value

document.getElementById(‘<%=txtTotalAmount.ClientID.ToString()%>”).value=Amount;

}

 

Get the grid using

var oGrid = igtbl_getGridById(tableName);

Get the row counte using

var RowsCounter = oGrid.Rows.length;

Loop through the grid, find the last column of the grid which is infact the amount column, add all the amounts

for(c = 0; c < RowsCounter; c++)

{

var row = oGrid.Rows.getRow(c);

if(row.getCell(1).getValue()!=null)

{

var AmountCellVal = row.getCell(3).getValue();

Amount += parseInt(GetAmount(AmountCellVal));

}

}

 

// Obtain the txtTotalAmount textbox and update its value

document.getElementById(‘<%=txtTotalAmount.ClientID.ToString()%>”).value=Amount;

 

Waiting for your comments on this article

Facebook Twitter Email

Leave a Reply

Your email address will not be published. Required fields are marked *