Using VS 2005:
I have a Datagrid or Gridview that will display Names and address and I want
to loop through them and collect the names to give to a javascript function.
I am trying to find the best way to do this.
At the moment, I am doing it through Page_PreRender where I just loop
through the grid using FindControl to get each name and append them together
in a hidden asp:Label control. I can then just use GetElementById to get
the string from the hidden label.
Sub Page_PreRender(ByVal Sender As Object, ByVal E As EventArgs)
Dim oLabel as WebControls.Label
for each oItem as DataGridItem in DataGrid1.Items
oLabel = CType(oItem.FindControl("FullName"),WebControls.Label)
if oLabel.Text = "" then SaveNames.Text = SaveNames.Text & "," &
oLabel.Text
Next
End Sub
Is there a way to this is Javascript using GetElementById or some other
function?
The Label is being turned into a link so that it looks something like:
<a id="ctl00_mContentPlaceHolder_dlResults_ctl01_FullName" class="name"
href="javascript:__doPostBack('ctl00$mContentPlaceHolder$dlResults$ctl01$FullName','')">Peter
Helton Do</a>
How can I loop through using Javascript to do the same thing when an event
if fired - or on entry to the page?
Thanks,
Tom
bruce barker - 04 Mar 2008 17:43 GMT
pretty trival:
function domWalk(node,rtn) {
var nodes = new Array();
if (rtn(node)) nodes[nodes.length] = node;
for (var i=0; i < node.childNodes.length; ++i)
nodes = nodes.concat(domWalk(node.childNodes[i],rtn));
return nodes;
}
// call on some click event
function onSomeClick() {
var grid = '<%=DataGrid1.ClientID%>';
var nodes = domWalk(grid,function(node) {
return (node.id && /FullName$/.test(node.id));
}
// do something with list of nodes
}
-- bruce (sqlwork.com)
> Using VS 2005:
>
[quoted text clipped - 34 lines]
>
> Tom
Mark Rae [MVP] - 04 Mar 2008 17:46 GMT
> Is there a way to this is Javascript using GetElementById or some other
> function?
Unless you're expecting the data in the GridView to be modified client-side,
I'd simply use this:
http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.regis
terarraydeclaration(VS.80).aspx
and populate it in the GridView's RowDataBound event.

Signature
Mark Rae
ASP.NET MVP
http://www.markrae.net