Obtaining the value of a command argument with JavaScript in ASP.NET

         <asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  BackColor="#589FC2"  CommandArgument='<%#Eval("BookingId")%>'  OnClientClick="jk();" >In progress</asp:LinkButton>

This LinkButton is present in a repeater for each row. I am looking to extract the value of the command argument using JavaScript. Any suggestions on how to achieve this?

Answer №1

If you want to incorporate html5 data attributes, here's a way to do it:

<asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  BackColor="#589FC2"  data-bookingid='<%#Eval("BookingId")%>'  CommandArgument='<%#Eval("BookingId")%>'  OnClientClick="jk();" >In progress</asp:LinkButton>

To access this data using JavaScript:

var lnkProgress = document.getElementById('<%= lnkProgress.ClientID %>');
var bookingID = lnkProgress.getAttribute("data-bookingid");

It's important to note that if your lnkProgress is within a naming container like a databound control, retrieving its reference may not be as straightforward as using

getElementById('<%= lnkProgress.ClientID %>');
.

Answer №2

<asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  
    BackColor="#589FC2" data-CommandArgument='<%#Eval("BookingId")%>'
    OnClientClick="jk();" >In progress</asp:LinkButton>

When using JavaScript:

document.getElementsId("lnkprogress")[0].getAttribute("CommandArgument")

You can refer to this link for more information.

If you prefer using jQuery:

$('#lnkprogress').data('CommandArgument');

Answer №3

To enhance the functionality of your ASP.NET LinkButton, consider adding an HTML5 attribute as suggested by rtpHarry and passing the control as a parameter in the OnClientClick event:

<asp:LinkButton ID="lnkprogress" runat="server" class="label label-info"  `BackColor="#589FC2"  data-bookingid='<%#Eval("BookingId")%>'  CommandArgument='<%#Eval("BookingId")%>'  OnClientClick="javascript: jk(this);" >In progress</asp:LinkButton>

Within your function, simply access the control attribute like this:

function jk(ctrl) {
            var id = ctrl.getAttribute("data-bookingid");
        }

Passing the control as a parameter eliminates concerns about the LinkButton's placement within another container, such as a GridView.

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

Interactive chat feature with live updates utilizing jQuery's $.Ajax feature for desktop users

I came across a script for real-time chat using $.ajax jQuery, but it only refreshes my messages. Here's an example scenario: I type: Hello to You, and I see this message refreshed. You reply: Hey, in order to see your message, I have to manually refr ...

Switching Image Values in JavaScript: A Quick Guide

After successfully using a JavaScript function to swap the src of two images, I encountered an issue. I also needed to switch two values associated with the images so that I could calculate them later. For example: img src="ble.jpg" id="F" value="" onclic ...

Remove search results in real-time

I'm currently working on implementing a search feature for a web application. While I have made some progress, I am facing an issue with removing items when the user backspaces so that the displayed items match the current search query or if the searc ...

Pass the response from a JavaScript confirm dialog to a Ruby program

Apologies if this question seems naive, but I am curious if it is feasible to pass the response from a JavaScript confirm box or message box as a value to a Ruby program. If I were to click on the "yes" button in the confirm box, could this response be st ...

Retrieve the HTML contents of a cell that contains a checkbox with the value of "jquery"

Here is an example of a table row: <tr> <td><input type='checkbox' name='post[]' value="1"></td> <td>08-Apr-2014</td> <td>injj team</td> <td>merchant.testyy.com</ ...

What could be causing my node-statsd client script to not terminate?

When attempting to log a metric to a StatsD server using the node-statsd library, I encountered an issue where the script did not exit automatically. The code snippet in question is as follows: var StatsD = require('node-statsd').StatsD; var cli ...

What is the reason behind installing both Typescript and Javascript in Next.js?

After executing the command npx create-next-app --typescript --example with-tailwindcss my_project, my project ends up having this appearance: https://i.stack.imgur.com/yXEFK.png Is there a way to set up Next.js with Typescript and Tailwind CSS without i ...

Is there a way to deactivate the <script> tag using CSS specifically for media queries?

When designing a website exclusively for desktop usage, I encountered the issue of it not being viewable on mobile devices. I attempted to address this problem by utilizing the code below: script { display: none; pointer-events: none; } Unfortunat ...

My Node setup is not displaying my scene with the THREE.js Software Renderer

Struggling to incorporate 3d graphics into Node.js environment, I stumbled upon the Software Renderer after exhaustive research. The renderer is up and running, but I am facing difficulties in rendering my scene. The issue lies in the fact that my 3d obje ...

Unable to resolve issue with Display:flex in my CSS, despite numerous attempts

Here is the structure of my CSS and HTML: #TopBar{ display: flex; justify-content: space-between; z-index: 1; position: fixed; top: 0px; left: 0px; background-color: rgb(25,25,25); height:115px; width: 2000px; } #Logo{ top: 0px; height: 110px ...

What is the Best Way to Send JavaScript Variables to MYSQL Database with PHP?

I am having trouble sending my variable to a MySQL database. The database only displays the variable when using the HTML input tag. The error message I received was "Undefined index: rate & amount." Seeking assistance to resolve this issue. Thank you! ...

Issue with JQuery: object.id is returning as undefined even though it should have a value

While working with JQuery, I encountered a rather peculiar (or possibly foolish) error. The HTML code snippet in question is as follows: <input type="password" name="repeatPassword" id="id_repeatPassword" /> And within my javascript code, I have t ...

Show a dynamic Swiper carousel upon selecting a thumbnail in an image gallery

I am in the process of creating a thumbnail gallery that includes a slider feature using Swiper. The default setup has the slider hidden, and once an image is clicked, the slider should appear with the selected image. To close the slider and return to the ...

uncertainty when implementing ng-if / ng-show / ng-hide

I am facing an issue with exporting content to PDF from my HTML. When a user clicks on the export button, the PDF is downloaded, but there are certain divs whose content I do not want to be exported or shown in the PDF. However, I still want them to be vis ...

Is it possible to initially design a login page using HTML/CSS and JavaScript, and then integrate VUE into it?

As part of a school project, I am tasked with developing a web-based application for a library system. The goal is to create a platform where librarians can login and manage books by adding, removing, or editing them. My responsibility lies in handling the ...

"Troubleshooting an issue with ng-model not functioning properly with radio buttons in Angular

I'm a newcomer to Angular and I'm attempting to retrieve the value of the radio button selected by the user using ng-model. However, I'm not seeing any output in "selected contact". Check out My HTML below: <!doctype html> <html n ...

Guide on incorporating dxTreeView with AngularJS

Hello there, I am trying to utilize the dx-tree-view component. In my Home.html file, I have included the following HTML code: <div dx-tree-view="treeViewOptions"></div> And in my HomeController.js: $scope.treeViewOptions = { binding ...

Expanding Gridview Width within a Container in ASP.Net: A Step-by-Step Guide

https://i.stack.imgur.com/ZaJE7.jpg After viewing the image above, I am facing a challenge with stretching and fixing a gridview to contain the entire div where it is placed. The issue arises when the gridview adjusts automatically based on the content&ap ...

Steps for accessing a local resource in an aspx file

I am relatively new to aspx (I have mainly worked with MVC in the past) and I may sound silly to some, but I am facing a bit of a challenge: I have a page called New.aspx located in a subfolder (Incident/New.aspx) and I need access to a resource file for g ...

I'm curious if someone can provide an explanation for `<->` in TypeScript

Just getting started with TypeScript. Can someone explain the meaning of this symbol <->? And, is ProductList actually a function in the code below? export const ProductList: React.FC<-> = ({ displayLoader, hasNextPage, notFound, on ...