Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Best way to get GridView/DataGrid data for Javascript

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 04 Mar 2008 17:05 GMT
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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.