Extract the initial line in the grid

I was wondering if there is a way to automatically copy the first value of a row in a grid to all the remaining rows. For example, if I have two columns A and B, and I input a value in column B at the first row, can that value be automatically populated in all other rows?

**Serial**   **CartNum**   Output **CartNum**
    1             1                    1
    2                                  1
    3                                  1
    4                                  1

Is this achievable? I am working with ASP.NET (VB) and JavaScript for this task.

Below is the code snippet for my grid:

<div class="Scrolls">
  <asp:DataGrid runat="server" ID="dgSerial" AutoGenerateColumns="false" ShowFooter="false" ShowHeader="false" GridLines="None" DataKeyField="BaseLineNum" AlternatingItemStyle-CssClass="AlternateGrid">
    <Columns>
      <asp:TemplateColumn>
        <ItemTemplate>
          <table border="0" style="border-bottom: 1px dotted; border-color: #FF0000">
            <tr>
              <td style="width: 20px;">
                <asp:CheckBox runat="server" ID="chkSelect"></asp:CheckBox>
              </td>
              <td style="width: 120px;">
                <asp:TextBox Width="120" ID="SerialNum" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SerialNum")%>' />
              </td>
              <td style="width: 120px;">
                <asp:TextBox Width="120" ID="CartNum" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CartNum")%>' />
              </td>
            </tr>
          </table>
        </ItemTemplate>
      </asp:TemplateColumn>
    </Columns>
  </asp:DataGrid>
</div>

I appreciate any insights or guidance on how to achieve this functionality. Thank you!

Answer №1

To implement the necessary changes, you need to adjust your second row template as shown below:

<asp:TextBox Width="120" ID="CartNum" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CartNum")%>'
  onchange="javascript: copy();" CssClass="row2"/>

Next, incorporate the Javascript functionality:

<script type="text/javascript>
  function copy() {
    var cells = document.getElementsByClassName("row2");
    for(var i = 1; i < cells.length; i++)
      cells[i].value = cells[0].value;
  }
</script>

I haven't been able to test this yet, so there may be some tweaking required for optimal performance.

Keep in mind that the script retrieves values from the first cell of the column (cells[0]), aligning with the specifics of your query.

Answer №2

Feel free to give this a try for assistance:

<script type="text/javascript">
function replicateName(){
    var inputCount = document.getElementById('tableId').getElementsByTagName('input').length;
    var name = document.getElementById('name').value;
    for(i=1;i<=inputCount;i++){
    document.getElementById('name_'+i).value = name;
    }
}
</script>
<table id="tableId">
<tr>
<td>
    <input type="text" id="name" onkeyup="replicateName();" name="name" />
    <input type="text" id="name_1" name="name_1" />
    <input type="text" id="name_2" name="name_2" />
    <input type="text" id="name_3" name="name_3" />
    <input type="text" id="name_4" name="name_4" />
</td>
</tr>
</table>

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

Tips for transferring a variable to another .php file using ajax

Is there a way to pass the id in posts.php using ajax while keeping the href value as "#"? Currently, when I click on this link, it redirects to that link. However, I do not want to navigate to the link and still need to pass the id in posts.php. Any sug ...

Import the controller, factory, service, and directive files using Browserify

As I set up my AngularJS application in gulp-browserify, I aim for a clean and organized base structure to accommodate its anticipated size. In the main entry javascript file for browserify, I establish the main angular module and configure routes with ui ...

Using an external HTML file to import a template into Vue.js single file components

I've been tackling a Vuejs project that involves using vue-property-decorator in single file components. I'm trying to figure out how to import the template from an external HTML (or different) file, but so far I haven't found a solution. I& ...

Using Javascript to group elements by JSON data

I have an array of JSON objects and I'm attempting to organize it by a specific element. I came across some code example on grouping JSON format from this link const groupBy = prop => data => { return data.reduce((dict, item) => { ...

Creating an event within a class that is generated dynamically when hovering

I'm trying to create a hover effect where an image moves around the page in a pattern (top left corner -> top right corner -> bottom right corner -> bottom left corner -> then back up to the top left corner). To achieve this, I am adding ...

Is there a straightforward method to retrieve all information from Vue.js?

Is there a way to extract all of this data? I've attempted using this._data.forEach but it doesn't seem to be effective. Thank you! data() { return { childData: '', credit: '', company: '', email: ...

Is utilizing React function components the most effective solution for this problem?

export default function loginUserUsing(loginType: string) { const [state, setState] = useState(loginType); function login() { // body of the login function } function oauth() { // body of the oauth function login(); ...

Troubleshooting issue with password validation not functioning correctly within the Joi schema in React

I am working on developing a password change form using React. Below is the code snippet of my component: import React, { Component } from "react"; import Joi from "joi-browser"; import "./Login/Login.css"; import { UAA } from ...

How can we avoid multiple taps on Ext.Button in Sencha Touch?

Currently working on a Sencha Touch game, but struggling with users tapping buttons multiple times. Looking for a simple solution to prevent multiple tap events in the app.js file so that only one tap event is executed even if a user taps or presses for an ...

What could be causing the <img src= ' '/> tag to malfunction in Express?

While learning about HTML, I noticed that simply using img src="...." worked fine. However, when working with Express, the same code did not work! The documentation mentioned that I needed to place the images in a folder named public, require(&ap ...

How can I align Javascript output vertically in the middle?

I am currently facing an issue with a JavaScript clock displaying in a narrow frame: <!DOCTYPE html> <HTML> <HEAD> <TITLE>Untitled</TITLE> <SCRIPT> function checkTime(i) { if (i < 10) { ...

Guide to importing Bootstrap 5 bundle js using npm

Having some issues with implementing Bootstrap5 and NPM. The website design is using bootstrap, which works fine, but not all the JavaScript components (dropdowns, modals, etc). I want to figure out how to import the Bootstrap JS bundle without relying on ...

Troubleshooting issue in App component while utilizing React-Redux's 'connect' function

Upon attempting to utilize the connect() function provided by react-redux, I encountered the following error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the r ...

Create HTML content on the fly and ensure its validity

I am attempting to dynamically add a TextBox with the ID "tbComment" when the selected date is less than the current date on the client side, prior to hitting the submit button. Which approach would be the most effective in achieving this? I have tried us ...

"Error encountered: Route class unable to reach local function in TypeScript Express application" #codingissues

Experiencing an 'undefined' error for the 'loglogMePleasePlease' function in the code snippet below. Requesting assistance to resolve this issue. TypeError: Cannot read property 'logMePleasePlease' of undefined This error ...

Issues with javascript and php carousel functionality not performing correctly

I am currently in the process of creating a slideshow/carousel for a website. Recently, I implemented a PHP for-loop to iterate through a folder of images, allowing me to use the slideshow with an unspecified number of images. However, after making this ch ...

Guide to linking an external URL with a lightbox image

I have created a gallery and an admin gallery page. In the admin gallery, there is a delete button that appears over the image. However, when I click on it, instead of showing the message "Are you sure?", it opens a lightbox. I suspect that the code for t ...

Is it necessary for me to create distinct MongoDB models in individual files?

Lately, my focus has been on working with mongodb using a single model. As I started exploring the idea of adding a second model, I anticipated encountering some challenges. To begin, here is the code for the single model: riskRating.js const mongoo ...

Guidelines for implementing a seamless translation effect with CSS3

CSS: .horizon { position: absolute; top: 380px; width: 1800px; height: 50px; background: url(images/road.png) repeat-x; -webkit-animation-name: prop-600; -webkit-animation-duration: 20s; -webkit-animation-iteration-count: i ...

Embedding Views in a <td> tag with DataTables and Ember.js

Has anyone ever attempted to incorporate a view within a using data tables? Currently, I am loading the data and dataTables through didInsertElement. Within each record, I am adding an "edit" and "delete" button inside a td so users can click to edit or d ...