Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.

How can I find the label text inside a DataList?
Is this even possible?

My DataList only displays 1 row, if in case it will display more than 1 row I would like to get only the first row.

Thanks for answering! Hooray!

ASP.NET
<asp:DataList ID="dlUserAccess" runat="server">
                                    <itemtemplate>
                                        Admin: <asp:Label ID="adminlabel" runat="server" Text='<%# Eval("Admin") %>'>
                                        Accounting: <asp:Label ID="accountinglabel" runat="server" Text='<%# Eval("Accounting") %>'>
                                        FinalApprover: <asp:Label ID="finalapproverlabel" runat="server" Text='<%# Eval("Approver") %>'>
                                        Benefits: <asp:Label ID="benefitslabel" runat="server" Text='<%# Eval("Benefits") %>'>
                                        FacultyProf: <asp:Label ID="FacultyProflabel" runat="server" Text='<%# Eval("FacultyProf") %>'>
                                        OtherExpense: <asp:Label 
ID="OtherExpenselabel" runat="server" Text='<%# Eval("OtherExpense") %>'>
                                        <br>


What I have tried:

I tried this but no luck -- .forChecking is a li in ul

JavaScript
$(function () {
         $('.forChecking').click(function () {
             //var str = $(this).text();
             var dl = $('#dlUserAccess label#benefitslabel')

             alert(dl);

             //$('#loading-content').load('dataSearch.php?' + str, hideLoader);
         });
     });
Posted
Updated 17-Nov-16 23:16pm
v4

1 solution

The easiest way is to assign a class attribute to your Label control like:

ASP.NET
<asp:label id="adminlabel" runat="server" text="<%# Eval("Admin") CssClass="labelClass" %>">
</asp:label>


Then in your jQuery, you can access it like this:

JavaScript
var theValue = $('.labelClass').text();
 
Share this answer
 
Comments
F-ES Sitecore 18-Nov-16 5:26am    
You would need something like

var theValue = $('.labelClass').eq(0).text();

to get only the first element
Vincent Maverick Durano 18-Nov-16 8:32am    
you're right. I missed the "only first row" thing. Thanks for that!
bjay tiamsic 20-Nov-16 19:12pm    
Hi. Thanks for the reply.

How does your code define that the label is inside the datalist?
bjay tiamsic 20-Nov-16 20:24pm    
Hey! It worked!
But what does eq(0) mean? And how does that jQuery code define that the label is in a datalist?
Vincent Maverick Durano 21-Nov-16 1:58am    
The eq(0) is a jquery selector that selects an element with a specified index. In this case we've assigned the index 0 to determine the first row element. For details see: http://www.w3schools.com/jquery/jquery_ref_selectors.asp

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