Retrieving values of numerous Control IDs within ASP.net Listview and transferring them to JavaScript

I am working on a project where I have multiple labels within a listview control on an ASP.net page. My goal is to access the values of these labels in JavaScript in order to display specific data for each label when the user hovers over it with a mouse-over effect. While I have successfully accessed the value of the first lblID by assigning it to a variable in JavaScript, I am now faced with the challenge of accessing the values of the remaining labels (assuming there are 10 records in the listview).

Each label will contain a unique value such as 1, 5, or 24, which needs to be passed to an ASP.net file via JavaScript in order to retrieve the corresponding text for the mouse-over effect. The ASP.net page sys_get_rankings.aspx?id=1 will display the HTML for the mouse-over.

The issue lies in dynamically retrieving the values from the changing number of labels every time. The purpose of the JavaScript is to generate the desired mouse-over effect when the user hovers over an element with the class 'button'.

Below are excerpts of the code:

The JavaScript Code

<script type="text/javascript">
$(document).ready(function(){
    $('.button').CreateBubblePopup({
        position: 'top',
        align: 'center',
        innerHtml: '<img src="images/loading.gif" style="border:0px; vertical-align:middle; margin-right:10px; display:inline;" />loading!',
        innerHtmlStyle: { color:'#FFFFFF', 'text-align':'center' },
        themeName: 'all-black',
        themePath: 'images/jquerybubblepopup-theme'
      });
    $('.button').mouseover(function(){
            var button = $(this);
            var x =  document.getElementById('ctl00_cpMain_ListView1_ctrl0_lblID').innerText;
            $.get('sys_get_rankings.aspx?Id=' + x, function(data) {
                button.SetBubblePopupInnerHtml(data, false); 
                };
            }); 
    }); 
});
</script>

HTML / ASP.net Code

<asp:ListView ID="lstUsers" runat="server" DataSourceID="sqldsUsers">
...
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; font-weight:
bold; color: #2661d1;" class="button"><%# Eval("Player_Name") %></span>

<asp:Label ID="lblID" runat="server" Text="" Visible="true" ><span style="color: #FFF">      
<%#Eval("TheUserID")%></span></asp:Label>
...
</asp:ListView>

I hope this explanation clarifies the situation and I welcome any insights or suggestions to address this challenge!

Answer №1

Here's my approach to tackling this issue.

To begin, assign a unique CssClass value to your Label controls (e.g., "label") for easy access without worrying about ASP.NET id values.

Next, in the javascript code for the button hover event, you can utilize something like button.next('.label') to locate and manipulate the corresponding label. This assumes that the .button span is positioned directly adjacent to the label.

From there, extract the innerText or any other required information.

Answer №2

Instead of directly embedding the user ID in your span element, consider adding a custom attribute called "userid":

<span userid="<%#Eval("TheUserID")%>" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; color: #2661d1;" class="button">

By using this approach, you can easily retrieve the user ID value in your JS code without having to rely on specific DOM structures:

var x = $(this).attr('userid');

This way, your solution remains flexible even if you decide to change how the user ID is displayed in the future.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error when using a third-party library in a jQuery plugin on an Android device

Currently, I am working on an application that utilizes a jQuery UI plugin, which then relies on the Raphael library. Everything runs smoothly on iOS and standard browsers, but when it comes to Android, I encounter the following error: ReferenceError: can ...

UpdatePanel Animation kicks in only after a Timeout

Let's consider this particular situation: Master Page File 1: <%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Frame.master.vb" Inherits="Project.Frame" ClientIDMode="Static" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...

Why does the Next.js GET index request keep fetching multiple times instead of just once?

Currently, I am encountering an issue while working on a tutorial app with Next.js. One of my components is not rendering due to what seems like multiple executions of a simple GET request for an index page. The problem perplexes me, and I need some assist ...

The jQuery each function in combination with the index function allows for

I'm struggling to make this jQuery script work properly: $(document).on('knack-scene-render.scene_126', function() { $('#view_498 > div.kn-list-content.columns.is-multiline').each(function(index) { if ($(this).eq(inde ...

Is there a way to retrieve php session in a javascript file?

Here's the code snippet for reference: Contents of index.php file Javascript code from index.php function Result() { var marks = 55; document.getElementById("hdnmarks").innerHTML= marks; window.location = "results.php"; } HTML code from ind ...

Selenium testing - Immediate disappearance of OK/Cancel popup

Something strange is happening here. When I manually test this, I click the delete button, wait for the popup to appear https://i.sstatic.net/6PMS4.png and then click OK to remove the record successfully. However, when I attempt the same process in Java/Se ...

Retrieve data from JSON using AJAX

I am working with an API that provides JSON data in the following format: [{ "Code": "001", "Name": "xyz", "Members": [{ "FullName": "User1" }] }, { "Code": "002", "Name": "asd", "Members": [{ "FullName": "User2 ...

Ways to refresh an entire page using the <Link> tag in react-router-dom

Can you please assist me with a problem I encountered with the Link component in react-router-dom? Here is the issue: window.addEventListener("scroll", function() { const theElement = document.getElementById("id-of-div"); const elementTop ...

When constructing URLs, opt for using the slash "/" instead of the question mark "?" for query strings

Working on a project using ASP.NET webforms, I am handling an aspx page called "Category(.aspx)". Within this page, I extract a query string labeled "categoryId" which functions as the primary key for categories in the database. However, the current URL s ...

The issue of the "port" attribute not working for remotePatterns in the Image component has been identified in Next.js 13's next.config.js

I've encountered an issue with the code snippet below. I'm attempting to utilize remotePatterns in my next.config.js file to enable external images. Strangely, when I set the port to an empty string "", it functions correctly. However, specifying ...

The custom attribute in jQuery does not seem to be functioning properly when used with the

I am currently working with a select type that includes custom attributes in the option tags. While I am able to retrieve the value, I am experiencing difficulty accessing the value of the custom attribute. Check out this Jsfiddle for reference: JSFIDDLE ...

Vue - a guide on customizing a prop in a parent component

I have implemented a wrapper component that encapsulates a Quasar q-select element in the following manner: <template lang="pug"> q-select( :options="organisations" option-value="id" v-bind="$attrs&quo ...

Extending a Typescript class from another file

I have a total of three classes spread across three separate .ts files - ClassA, ClassB, and ClassC. Firstly, in the initial file (file a.ts), I have: //file a.ts class ClassA { } The second file contains: //file b.ts export class ClassB extends Class ...

I am interested in utilizing Sequelize, Vue, and Node to implement a query that filters data once it is retrieved to display on my view

Within my project, there's a table tracking user responses to a quiz. By utilizing the following query, I've successfully retrieved all the information from that table and stored it in my TotalQuizResponses array for display: this.totalQuizRespon ...

Having trouble locating the specified spreadsheet within an Excel document when attempting to extract data from a website using VB.Net

I'm currently working on a project to create a webpage that enables users to select an excel file. The webpage will then read the file contents and upload the data to a database after validation. On the page, I have implemented a fileUpload asp contr ...

Data has been successfully acquired through the query, however, it cannot be accessed via the GraphQL API

After successfully building the API with Apollo Server and verifying its functionality in GraphiQL, I proceeded to make requests to the API from a front-end React app using Apollo Client. const [getUserPosts, { loading, error, data }] = useLazyQuery(GET_US ...

How to automatically center Google Maps and update marker on responsive resize

Trying to figure out how to maintain the center of Google Maps while resizing responsively has been a challenge. I've successfully centered the map, but I'm struggling to include my marker in the JavaScript code so that it stays centered as well ...

What are the steps for implementing claim-based authentication in Windows Phone 7?

Currently, I am in the process of developing a Windows Phone 7 application and exploring claim-based authentication for the first time. To assist me with this, I have been referring to the following link which explains how to implement claim-based authenti ...

Issues Arising from AJAX ScriptManager

I am currently facing an issue with setting up a ScriptManager in .NET 3.5 as I keep encountering a scriptmanager exception. In order to resolve this, I have removed all UpdatePanels from the page and restructured everything into UserControls to ensure tha ...

"Design the website with a WYSIWYG editor while preventing users from disrupting the page's

I am considering using an HTML WYSIWYG editor like CKEditor, but I am concerned about the possibility of users submitting HTML code that could alter the layout of the page when rendered. Here is a comparison between two posts: <p><b>This is m ...