I have below lines of code in asp:list view control, if user input any value on 2nd text box, on client side lost focus event I need to calculate txtTokensReq * txtQty and display in lblTotalTokens.
WHen I wrote onblur function on 2nd textbox, it is firing more than once.
Thanks in advance.
Tuhin PaulPosted May 31, 2024, 2:30 AM
assume that each row in your table has a unique identifier stored in a data attribute (e.g., data-row-id). Adjust the code according to your specific HTML structure and requirements.
Tuhin PaulPosted May 31, 2024, 2:29 AM
To get a reference to the row when a user enters the Quantity and clicks on Tab, you can utilize event delegation. Event delegation allows you to attach an event listener to a parent element (like a table) and then handle events for its child elements (like rows or cells). This way, you can dynamically determine which row the event originated from.
Prasad RaveendranPosted May 29, 2024, 12:37 AM
To achieve this, you can use JavaScript to handle the
onblurevent of the secondTextBox(txtQty) and calculate the total tokens required. Then, update the correspondingLabel(lblTotalTokens) within the same row of theListView. Here is a step-by-step solution:onblurEvent Handler totxtQty: Attach anonblurevent totxtQtyto trigger the calculation when the user leaves the textbox.txtQty.lblTotalTokens: Multiply the values oftxtTokensReqandtxtQty, then update thelblTotalTokenswith the result.Here is the updated ASP.NET code with the JavaScript function:
Explanation:
onblurevent is added directly totxtQtyusing theonblur="calculateTotalTokens(this)"attribute.calculateTotalTokens):calculateTotalTokensfunction receives thetxtQtyelement that triggered the event.closest('.row').txtTokensReqandlblTotalTokenselements usingquerySelector.txtTokensReqandtxtQty, converts them to numbers, performs the multiplication, and updateslblTotalTokenswith the result.This solution ensures that the calculation is performed correctly and only once per user interaction. The use of
closestandquerySelectorensures that the JavaScript code is focused on the specific row where the event was triggered, thus avoiding any potential issues with duplicate control IDs.MohanPosted May 27, 2024, 7:58 PM
Thank you Namish & Jayraj, but the main question here is how to get the reference to the row when a user enters the Quantity and click on Tab?
Jayraj ChhayaPosted May 27, 2024, 6:02 AM
To address the multiple firing of the onblur event in the second textbox, you can ensure that the event is attached only once. One way to achieve this is by using jQuery to bind the event.
By binding the blur event using jQuery in this manner, you can ensure that the event is attached only once, preventing multiple firing issues. This approach should help you calculate the total tokens and display the result accurately in the
lblTotalTokenslabel on the client-side.Naimish MakwanaPosted May 27, 2024, 5:19 AM
Certainly! To achieve the desired behavior of calculating
txtTokensReq * txtQtyand displaying the result inlblTotalTokenswhen the user inputs a value in the second textbox and loses focus, you can follow these steps:Client-Side Event Handling:
onblurevent directly on the second textbox, we’ll attach an event listener to it.JavaScript Implementation:
txtQty) in your JavaScript code.txtQtytextbox for theblurevent.txtTokensReqandtxtQty, and update thelblTotalTokensaccordingly.Here’s an example of how you can achieve this:
txtTokensReq,txtQty, andlblTotalTokens) match the actual IDs in your markup.Remember to adjust the code according to your specific scenario and any additional requirements you might have. If you encounter any issues or need further assistance, feel free to ask! ??
References:
Thanks