javascript referencing datagrid

G

Guest

I have a table within a cell of a datagrid. I am doing updates without
postback to the server using Javascript. I have everything working, except
referencing a table within the datagrid cell.

Here is my datagrid code for the cell;
<asp:TemplateColumn HeaderText="3Fields" ItemStyle-Font-Size="8"
SortExpression="Field1 ASC" ItemStyle-Font-Name="Verdana">
<ItemTemplate>
<table border="0">
<tr>
<td id="Field1"><font size="0" face="Verdana"><b><%#
DataBinder.Eval(Container.DataItem, "Field1") %></b></font>
</td>
</tr>
<tr>
<td><font size="0" face="Verdana"><b><%# DataBinder.Eval(Container.DataItem,
"Field2") %></b></font>
</td>
</tr>
<tr>
<td><font size="0" face="Verdana"><b><%# DataBinder.Eval(Container.DataItem,
"Field3") %></b></font>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>

My javascript for referencing the data looks like this;
function UpdateComments(nField1,nField2)
{
for(counter=1;counter<parseInt(NumberOfRecords.outerText)+1;counter++)
{
alert(MyDataGrid.firstChild.childNodes(counter).childNodes(2).field1.innerText)
if (MyDataGrid.firstChild.childNodes(counter).childNodes(2).innerText ==
nfield1) and
(MyDataGrid.firstChild.childNodes(counter).childNodes(2).innerText == nfield2)
{
MyDataGrid.firstChild.childNodes(counter).childNodes(9).innerText= "Comments
Entered"
}
}
}

I have added an alert to see the value of what I am trying to compare to.
The field1.innerText comes up as an error with null or doesn't exist. If I
just reference the innertext without the project ID, I get all three fields.
I am just trying to reference the first.

How can I reference individual cells within a datagrid cell?

Thank you,
Lyners
 
G

Guest

Hi Lyners,

1- add Runat="server" to your table cells that have Ids “fiedl1†and “field2â€
2- add the following javascript function in your page

function FindControls(ControlID, TagName)
{
var ret= new Array(); //an array of all object of tagname within
//the datagrid whose ID = ControlID
var aControls = document.getElementsByTagName(TagName);
if (aControls)
{ for (var i=0; i < aControls.length ; i++)
{
if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
ControlID.length)
{

ret.push(aControls);
}

}
}
return ret;
}

3- make the following call to get a collection of the tablecells named field1
var controls = FindControls("field1","td");

4- to update the first row in the grid with newval you would
call controls[0].innerText = newval;
 
B

Bruce Barker

you need to learn javascript syntax. arrays use []'s, not ()'s. the logical
and is &&.
 
J

Jay Douglas

I have not run your example, but it maybe possible that the datagrid is
changing the <td id="Field1"> ... I'd check the output of the datagrid html
has a first troubleshooting step.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top