Friday, March 23, 2007

Binding Datagrid values at client side to controls

< asp:DataGrid ID="dgdPackageDetails" Runat="server" width="100%" AllowSorting="False" GridLines="None" AutoGenerateColumns="False" >
< Columns >
< asp:TemplateColumn >
< HeaderTemplate >
Date
< /HeaderTemplate >
< ItemTemplate >
< a title="< %# DataBinder.Eval(Container.DataItem,"Date","{0:d}")% > " href="#" onclick="displayPackageDeatils()" >
< %# DataBinder.Eval(Container.DataItem,"Date","{0:d}")% >
< /a >
< /ItemTemplate >
< /asp:TemplateColumn >
< asp:TemplateColumn >
< HeaderTemplate >
Name
< /HeaderTemplate >
< ItemTemplate >
< a title="< %# DataBinder.Eval(Container.DataItem,"Name")% > " href="#" onclick="displayPackageDeatils()" >
< %# DataBinder.Eval(Container.DataItem,"Name")% >
< /a >
< /ItemTemplate >
< /asp:TemplateColumn >

< /Columns >
< /asp:DataGrid >

/***************************************************************************
Name : displayPackageDeatils
Parameters : None
Purpose : To display the contents of the grid in the respective controls
***************************************************************************/
function displayPackageDeatils()
{
var objForm=document.frmPackageInput; // frmPackageInput is the id of the form
objForm.txtDate.value=trimAll(event.srcElement.parentNode.parentNode.cells(0).innerText) //where txtDate is id of a textbox
objForm.txtPackageName.value= trimAll(event.srcElement.parentNode.parentNode.cells(1).innerText)//where txtPackageName is id of a textbox
}
/***************************************************************************
Name : trimAll
Parameters : The string that is to be trimmed
Purpose : To perform both left and right trim of a given string
***************************************************************************/
function trimAll(sString)
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}

5 comments:

Anonymous said...

what is Container.DataItem

Anonymous said...

it will display data from grid to Controles or Reverse..?

Ashwin Kumar H said...

It will display data from grid to controls

Ashwin Kumar H said...

Container.DataItem is a data binding statement which produces a string output based on the value of the particular database column name.

Ashwin Kumar H said...

Container.DataItem is a data binding statement which produces a string output based on the value of the particular database column name.