PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

Best way to get GridView/DataGrid data for Javascript

 
 
tshad
Guest
Posts: n/a
 
      4th Mar 2008
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




 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      4th Mar 2008
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)


"tshad" wrote:

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

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      4th Mar 2008
"tshad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.registerarraydeclaration(VS.80).aspx

and populate it in the GridView's RowDataBound event.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview textbox has data check without postback.. javascript? wildman@noclient.net Microsoft ASP .NET 2 14th Feb 2008 04:29 AM
GridView and JavaScript Paulo Microsoft ASP .NET 0 4th Sep 2007 07:18 PM
How to see GridView data in JavaScript? Steve Kershaw Microsoft ASP .NET 2 19th Jul 2007 11:27 PM
JavaScript on GridView!! Vai2000 Microsoft ASP .NET 2 5th May 2006 02:43 PM
Javascript Error on a GridView templated column where data can contain single quote Anonieko Microsoft ASP .NET 2 10th Apr 2006 04:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:53 AM.