Best way to get GridView/DataGrid data for Javascript

T

tshad

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
 
B

bruce barker

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,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)
 

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

Top