Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If anyone can help me, have a post here, please give me some direction. I believe its easier to just post the link, than to rewrite to whole thread again. Thanks.

http://forums.asp.net/t/1906515.aspx/1?Getting+ListView+ul+li+asp+label+values+into+a+javascript+array
Posted

Hi Sergey, I have the solution! Thank you very much for all of your input, and for pushing me to develop the solution. This code could probably be shortened a bit, but it is working. I ended up changing the
    to a , which made it easier to traverse the DOM using the
    classname, and the class names that I assigned to my labels. One thing that I found out was, if a labels "Visible" property is set to false,(some values I did not want to show the user) .Net will not render it in the browser. So, to get around this, I set the labels Visible property to "true", and I juse set the Css to display:none; This worked good. Here is the original HTML, and the scripts. Thank you very much for the links Sergey!
    ASP.NET
    <!-- DataList holds locations returned from DB -->
            <asp:datalist id="healthCenters" cssclass="hcListing" runat="server" xmlns:asp="#unknown">
            EnableViewState="False" DataKeyField="LocationID">
            <itemtemplate>
            <table class="locations" border="0">
            <tr class="details">
              <td class="locationDetails"><asp:hyperlink cssclass="hcName" id="hlCenterName" runat="server" text="<%# Eval("CenterName") %>" navigateurl="<%# Eval("LocationID", "searchResponse.aspx?LocationID={0}") %>"></asp:hyperlink><br />
            Address: <asp:label cssclass="address" id="lblAddress" runat="server">
                      Text='<%# Eval("Address") %>' /><br />
            City/State:     <asp:label cssclass="city" id="lblCity" runat="server" text="<%# Eval("City") %>" /> , 
                            <asp:label cssclass="state" id="lblState" runat="server" text="<%# Eval("State") %>" /><br />
            Zip:            <asp:label cssclass="zip" id="lblZipCode" runat="server">
                               Text='<%# Eval("ZipCode") %>' /><br />
            Phone:          <asp:label cssclass="phone" id="lblPhoneNumber" runat="server">
                               Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(Eval("PhoneNumber"))) %>' /><br />
            Web:            <asp:hyperlink cssclass="url" id="webSite" runat="server" navigateurl="<%# Eval("Website") %>">
                               Text='<%# Eval("Website") %>'>HyperLink</asp:hyperlink><br />
            Patient Services:<asp:label cssclass="services" id="lblPatientServices" runat="server">
                                Text='<%# Eval("PatientServices") %>' /><br />
            Distance:        <asp:label cssclass="distance" id="lblRadius" runat="server" visible="True">
                                Text='<%# String.Format("{0:0.##}", Eval("DistanceMiles")) + " " + "miles." %>' /><br />
                             <asp:label cssclass="locationId" id="lblLocationID" runat="server" visible="true">
                                Text='<%# Eval("LocationID") %>' /><br />
                             <asp:label cssclass="lat" id="lblLat" runat="server" visible="true" text="<%# Eval("Lat") %>" /><asp:label cssclass="lng" id="lblLng" runat="server" visible="true" text="<%# Eval("Long") %>" />
                             <br />
            </asp:label></asp:label></asp:label></asp:label></asp:label></asp:label></td>
            </tr>
            </table>
        </itemtemplate>
            </asp:datalist>


    //the javascript
    C#
    function AddData() {
    
        var locationAry = [];
        //Use the jQuery .each() function to get each item
        $("td.locationDetails").each(function (i, elem) {
            var liListContent = {};
            liListContent.centerName = $(this).find(".hcName").html();
            liListContent.centeraddress = $(this).find(".address").html();
            liListContent.centerCity = $(this).find(".city").html();
            liListContent.centerState = $(this).find(".state").html();
            liListContent.centerZip = $(this).find(".zip").html();
            liListContent.centerPhone = $(this).find(".phone").html();
            liListContent.centerDistance = $(this).find(".distance").html();
            liListContent.centerId = $(this).find(".locationId").html();
            liListContent.centerLatitude = $(this).find(".lat").html();
            liListContent.centerLongitude = $(this).find(".lng").html();
            locationAry[i] = liListContent;
        });
        for (var i = 0; i < locationAry.length; i++) {
    
    //        for (var prop in locationAry[i]) {
                //alert("prop: " + prop + " value: " + locationAry[i][prop]);
    
            var title = locationAry[i].centerName;
            var address = locationAry[i].centeraddress;
            var city = locationAry[i].centerCity;
            var state = locationAry[i].centerState;
            var zip = locationAry[i].centerZip;
            var phone = locationAry[i].centerPhone;
            var distance = locationAry[i].centerDistance;
            var latitude = parseFloat(locationAry[i].centerLatitude);
            var longitude = parseFloat(locationAry[i].centerLongitude);
            var pinLocations = new Microsoft.Maps.Location(parseFloat(locationAry[i].centerLatitude), parseFloat(locationAry[i].centerLongitude));
            var description = locationAry[i].centeraddress + '<br/>' + locationAry[i].centerCity + '<br/>' +
                 locationAry[i].centerState + '<br/>' + locationAry[i].centerPhone + '<br/>' + locationAry[i].centerDistance;
    
    
            for (p = 0; p < locationAry.length; p++) {
            p = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(parseFloat(locationAry[i].centerLatitude), parseFloat(locationAry[i].centerLongitude)));
            p.Title = title;
            p.Description = description;
            Microsoft.Maps.Events.addHandler(p, 'click', displayInfobox);
            dataLayer.push(p);
                        }
    
            }
    
        function displayInfobox(e) {
            if (e.targetType == 'pushpin') {
                infobox.setLocation(e.target.getLocation());
                infobox.setOptions({ visible: true, showPointer: true, title: e.target.Title, description: e.target.Description });
            }
        }
    }
 
Share this answer
 
You can use jQuery: http://api.jquery.com/children/[^].

This is one of traversing functions; please see: http://api.jquery.com/category/traversing/[^].

Alternatively, you can use some other jQuery selector by some property which is common to all children but not other HTML elements on the page, such as CSS class (using a class selector) or having the same parent (with the effect similar to the traversing case shown above). Naturally, it should not be the id selector is the id attribute implies uniqueness of its value (I refer to the mistake done by one of the inquirers, don't repeat it). Please see:
http://api.jquery.com/category/selectors/[^],
http://api.jquery.com/class-selector/[^],
http://api.jquery.com/child-selector/[^].

The production of those methods will give you some collection of XML DOM elements (in jQuery wrapper).

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery[^],
http://learn.jquery.com[^],
start here: http://learn.jquery.com/about-jquery/how-jquery-works/[^].

—SA
 
Share this answer
 
Comments
mfisher8 17-May-13 9:19am    
Thank you for your reply, I do plan to learn jquery, but for now, I would like to retrieve these values from my HTML. What is the difference between javascript & jquery? Here is my HTML:

<asp:ListView ID="healthCenters" runat="server" DataKeyNames="CenterName,LocationID,Lat,Long">
<layouttemplate>
<ul id="myList">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>

<itemtemplate>
<li><asp:HyperLink ID="hlCenterName" runat="server" Text='<%# Eval("CenterName") %>' NavigateUrl='<%# Eval("LocationID", "searchResponse.aspx?LocationID={0}") %>' ></li>
<li style="list-style:none">Type of Center: <asp:Label ID="lblTypeofCenter" runat="server" Text='<%# Eval("TypeofCenter") %>' /></li>
<li style="list-style:none"><asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>' /></li>
<li style="list-style:none"><asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>' /> , <asp:Label ID="lblState" runat="server" Text='<%# Eval("State") %>' /></li>
<li style="list-style:none"><asp:Label ID="lblZipCode" runat="server" Text='<%# Eval("ZipCode") %>' /></li>
<li style="list-style:none">Phone:<asp:Label ID="lblPhoneNumber" runat="server" Text='<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(Eval("PhoneNumber"))) %>' /></li>
<li style="list-style:none"><asp:HyperLink ID="webSite" runat="server" NavigateUrl='<%# Eval("Website") %>' Text='<%# Eval("Website") %>'>HyperLink</li>
<li style="list-style:none">Patient Services: <asp:Label ID="lblPatientServices" runat="server" Text='<%# Eval("PatientServices") %>' /></li>
<li style="list-style:none">Distance: <asp:Label ID="lblRadius" runat="server" Visible="True" Text='<%# String.Format("{0:0.##}", Eval("DistanceMiles")) + " " + "miles." %>' /></li>
<li style="list-style:none"><asp:Label ID="lblLocationID" runat="server" Visible="false" Text='<%# Eval("LocationID") %>' /></li>
<li style="list-style:none"><asp:Label ID="lblLat" runat="server" Visible="False" Text='<%# Eval("Lat") %>' /><asp:Label ID="lblLng" runat="server" Visible="False" Text='<%# Eval("Long") %>' /></li>
<br />

Sergey Alexandrovich Kryukov 17-May-13 9:27am    
You could read that jQuery is merely a JavaScript library. So, your "for now" does no make whole lot of sense, if would simply mean that you would need to write something which is already written in jQuery very well and fully tested by many people.
—SA
mfisher8 17-May-13 9:30am    
But I noticed that some of the syntax is different, I am new to javascript, and I have never used the $ sign. Sorry, I did not know that jquery is a javascript library, its why I'm coming here for help, to learn. Thank you.
Sergey Alexandrovich Kryukov 17-May-13 9:46am    
It is not different. If you are new to JavaScript (and even not very new) jQuery looks a bit weird. This is poor JavaScript syntax, and '$' is just a valid but unusual identifier name taken up by the library. This is the name of some very universal jQuery function which tries to build a complex wrapper object out or any string...
—SA
mfisher8 17-May-13 9:32am    
I just want to know, which HTML element should I be pulling my values from? The <li> or the <asp:> elements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900