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

Creating a Dynamic Clear Button for a Text Area in Angular

Working on my Angular application, I have implemented a form with a textarea element. My goal is to incorporate a clear button inside the textarea element that should: Appear only when the textarea is focused Disappear when the textarea is out of focus ( ...

When a named capture group is included in the regex of a GET path, Express crashes with the error message: "Cannot read property 'name' of undefined at Layer

I am looking to process a GET request and extract specific information from the URL. Let's consider this scenario: const REGEX_QUERY = /^\/?house\/(?<street>[a-z]+)\/(?<house>[0-9]+)$/i; const REGEX_QUERY_NO_NAMES = /^\ ...

What is the best way to retrieve the border-color inline style using jQuery?

I have a HTML tag like this. <span id="createOrderFormId:accountNo" style="border-color: red;"><</span> To retrieve the style value for the border-color property, I tried using the following jQuery code: $( document ).ready(function() { ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

Please provide TypeScript code for a React wrapper function that augments a component's props with two additional functions

During the course of my project, I implemented a function wrapping React component to incorporate undo/redo functionality using keyboard shortcuts Ctrl+Z and Shift+Ctrl+Z. Here is an example: import React from 'react'; interface WithUndoRedoProp ...

Removing unnecessary keys from intricate JSON data (with the use of pure JavaScript)

I've experimented with various methods to dynamically parse and remove keys with empty values from a JSON file using JavaScript. Currently, I can successfully delete non-nested keys, except for empty strings that have a length greater than 0. My mai ...

How can the 'selected' attribute be assigned to 'select/option' tags depending on the option value?

In my code, I have a select input with various options and values. I want to set the 'selected' attribute for the option that corresponds to a specific value X. Here is an example: // If x=3, I want the option with value=3 to be marked as 's ...

Implementing asynchronous validation in Angular 2

Recently started working with Angular 2 and encountered an issue while trying to validate an email address from the server This is the form structure I have implemented: this.userform = this._formbuilder.group({ email: ['', [Validators.requir ...

"The JavaScript code that functions perfectly in the browser console, but fails to execute when running in the actual

I'm encountering an issue with a simple piece of JavaScript code that seems to only work when executed in the browser console: <script> $(".hopscotch-close").click(function () { alert("Hi"); Cookies.set("tourState", "closed" ...

Developing web applications using a combination of PHP, MySQL, and

I am in need of creating an HTML form that functions as CRUD. This form should be able to record inputs such as "row_number", "channel_name", and "ip_address". It will store all the data in a MySQL table using the "ORDER BY row_number" function. The form i ...

Matching a regular expression pattern at the beginning of a line for grouping strings

One of my tasks involves working with a markdown string that looks like this: var str = " # Title here Some body of text ## A subtitle ##There may be no space after the title hashtags Another body of text with a Twitter #hashtag in it"; My goal ...

Is it possible to restrict contenteditable elements to only accept numbers?

Is there a way to restrict contenteditable elements such that only numerical values can be entered? I attempted to use the following code snippet: onkeypress='return event.charCode >= 48 && event.charCode <= 57' However, despite a ...

What steps should I take to verify the validity of an Angular form?

I am struggling with validating an inscription form in HTML. Despite trying to implement validations, the inputs are still being saved in the database even when they are empty. Here are the validations I want to include: All inputs are required Email addr ...

Unable to access the .env file in Vue.js when executing cross-env NODE_ENV=development webpack-dev-server --open --hot

I'm attempting to utilize an .env file for storing local variables, but I am encountering an issue where they appear as undefined when I try to log them. Here is a snippet from my .env file (located at the root of my project): VUE_APP_STRAPI_HOST=htt ...

Experiencing difficulties with JavaScript/jQuery when encountering the 'use strict' error

I have been working on a small function to change the background of an HTML file, but I keep getting the 'Missing 'use strict' statement' error message from JSLint despite trying multiple solutions. Can anyone suggest how to resolve th ...

The submission of the form using AJAX is currently experiencing issues

My current issue involves ajax not functioning as intended. I have a form that is being submitted through ajax to my php file for the purpose of updating my database. The database update is successful, but there seems to be a problem with the ajax function ...

How do I set the initial state to a specific node in xstate?

I'm currently working on a multi-step form that guides users through selecting services, providing contact information, and entering billing details. I have implemented a progress bar and event emissions to track the user's current step using xst ...

Unable to locate the root element for mounting the component in Cypress with React

I am currently testing my react app, which was created using create-react-app, with the help of cypress. Unfortunately, I encountered an error that looks like this: https://i.stack.imgur.com/xlwbo.png The error seems to be related to trying to fetch an ...

Encountering the error message "Unable to access /" on the browser when using express router

Just started working with the express Router for the first time. Here is my route.js: var express = require('express'); var router = express.Router(); router.get('/', function(req, res) { res.send('home page'); }); module.e ...

Utilizing cloud functions to distort an inappropriate image

I have a requirement to analyze any uploaded image for inappropriate content and blur it if necessary. The log this image is inappropriate indicates that the detection process is working correctly. However, I am not able to see any further logs which sugg ...