Guide on accessing and setting values in GridView Cell using JavaScript

Within my layout, I have two labels named FixedTotal and TotalPercent, as well as a single row GridView with columns labeled ReferenceID, Percentage, and Amount.

    ReferenceID       Percentage          Amount
   --------------    -------------       --------
        1                5                 1000

My goal is to update the "Percentage" column value of 5 with the text from the TotalPercent label, and to display the "Amount" column value of 1000 in the FixedTotal label. Additionally, I need to verify whether the GridView contains any rows. How can I accomplish this task given that the columns are BoundField Columns?

Answer №1

Manipulating data in gridview cells requires knowledge of the Row Index of the gridview. To achieve this, you can provide the row index as a parameter when invoking a JavaScript function from the gridview.

<script language="javascript" type="text/javascript">
function update(rowIndexOfGridview) {
    var ri = rowIndexOfGridview; 
    var grd = document.getElementById('<%= GridView1.ClientID %>');

    CellValue = grd.rows[ri].cells[1].childNodes[0].value; // get
    grd.rows[ri].cells[2].childNodes[0].value = CellValue; // assign
    ...........
    .............
}

Answer №2

<script language="javascript" type="text/javascript"> 
function Calculate() 
<br/>
{ 
<br/>
var grid = document.getElementById("<%=GridID.ClientID%>");
<br/> 
var total = 0; <br/>
for (var j = 1; j < grid.rows.length; j++)<br/>
 { <br/>
var CellValue = grid.rows[j].getElementsByTagName("input"); 
<br/>if (!CellValue[4].value) {total  += 0; } else { total += parseFloat(CellValue[4].value);} } 
<br/>
document.getElementById("<%=TextBox1.ClientID%>").value = total; 
 }
<br/>
 </script>
------------------------------------------------------------------------



<asp:TemplateField HeaderText="Current payment" >
                            <ItemTemplate>
                                <asp:TextBox ID="cridnvalue" runat="server" Width="70px" BorderStyle="None" onkeyup="CalculateTax();" ></asp:TextBox>
                            </ItemTemplate>
                            <ItemStyle Width="120px" />
                        </asp:TemplateField>

Answer №3

Feel free to give this a try. I personally tested it and encountered no errors.

<script language="javascript" type="text/javascript">
function updateRowInGridView(index) {
    var rowIndex = index; 
    var grid = document.getElementById('<%= GridView1.ClientID %>');

    var cellValue = grid.rows[rowIndex].cells[1].childNodes[0].textContent); // retrieve
    grid.rows[rowIndex].cells[2].childNodes[0].textContent = cellValue; // assign
}
</script>

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

What is the process for adding a dot to the package.json file using the npm-pkg command?

I need help using the npm pkg set command to create a package.json file with the following structure: { ... "exports": { ".": { "import": "./dist/my-lib.js", "require": "./dist/my-l ...

Node.js: The Ultimate Solution for Automatic Data Saving

Currently, I am working on a collaborative project using Node.js, MySQL, and React. I have encountered some difficulties with the ToDo list section. I would appreciate any advice on the best and quickest method to automatically save data or options to the ...

How to properly format JSON responses in Node.js or Express

I came across a question on Proper way to return JSON using node or Express and copied it for reference. I am looking for the response in a specific format. This is the sample format for the response API: { "success":true, "code":200, "message":"Ok", "da ...

The full screen menu is exclusively displayed in the navigation bar

My HTML and CSS code is causing an issue where the full-screen menu only appears in the nav bar. This problem seems to be specific to Safari. To solve the issue, I need to remove the following code: .nav { position: fixed; } However, I still ...

Submitting a file using Ajax XMLHttpRequest

Currently, I am facing an issue while attempting to send a file using the following XMLHttpRequest code. var url= "http://localhost:80/...."; $(document).ready(function(){ document.getElementById('upload').addEventListener('cha ...

Tips for assigning a standard or custom value using JavaScript

What is the best way to automatically assign a value of 0 to my input field when the user leaves it blank? Should I use an if statement { } else { } ...

Dynamic Loading of CSS Styles

My style-sheet is saved in this unique location: /opt/lampp/htdocs/project/core/styles/Style.css To load the CSS file using the full directory, considering that the files utilizing it are situated here: /opt/lampp/htdocs/project/client/ The ultimate ob ...

Error occurs during datetime parsing due to a Format Exception

After changing the system date format to dd/mm/yyyy, the web application started throwing a format error. The error message is attached below: Everything works fine when the system has the default date format. I also tried formatting the string but that d ...

What is the best way to access and utilize the data stored in my database using mySQL, PHP, and JavaScript?

My Objective : When a user selects a location from the dropdown list, I want the function "newMark()" to place a marker on Google Maps; Current Achievements : The map is displayed along with the select field containing all locations saved in my database. ...

Apply a specific style conditionally using Angular's ng-repeat directive

Currently, I am utilizing ng-repeat to iterate through a list of divs and manually adding items to the JSON that is rendering these divs. My goal is to position the last div that I add in the JSON at the location where the mouse cursor is, while keeping th ...

Navigating a secure Koa authentication flow using compose mechanism

I have this isAuthenticated function in expressjs that composes middleware into one. Now, I need to achieve the same functionality in Koa as I am migrating from Express. How can I replicate this in Koa? import compose from 'composable-middleware&apos ...

Bringing a module into Vue framework and transferring information

I'm currently working on a Nuxt project that includes a component. The component can be found in components/Boxes.vue: <template> <b-container> <b-row> <b-col v-for="box in boxes" v-bind:key="box"> < ...

Encountering difficulty when trying to define the onComplete function in Conf.ts. A type error is occurring, stating that '(passed: any) => void' is not compatible with type '() => void'.ts(2322)'

I have been developing a custom Protractor - browserstack framework from the ground up. While implementing the onComplete function as outlined on the official site in conf.ts - // Code snippet to update test status on BrowserStack based on test assertion ...

Looking for an alternative method since jQuery has deprecated the use of '.toggle()' function

After jQuery deprecated the .toggle() method, I have been searching for a new and simple solution to implement a "Read more" button that slides down a paragraph while changing text to "Read less". Below is the code I have put together: var moreText = "Re ...

Unveiling the Magic: Enhancing Raphaeljs with Interactive Click Events on a Delicious Slice of the

I'm having trouble responding to a click event on each slice of a Raphael Pie Chart. I've tried implementing the code below, but it doesn't seem to be working. The code is just two lines, commented as "My Code", in the example from the offic ...

"Utilize binding in ReactJS to maintain the correct context

I'm currently learning how to update states in ReactJS by following a tutorial. In the tutorial, I came across this line of code: this.updateLanguage = this.updateLanguage.bind(this). Although I grasp the basics of 'this' and 'bind&apos ...

Installing a specific version of yarn on Ubuntu

Is there a way to install yarn version 0.27.5 in Ubuntu despite the fact that the latest update is for version 1.2.1? ...

Navigating with the keys is disabled until an item is chosen

Currently working on a Shopify website for a client and encountered an issue where the keys don't scroll down upon page load unless a specific element is clicked or tab is used. I am not sure what caused this unexpected behavior, it may have occurred ...

On the second attempt to call setState within the componentDidMount method, it is not functioning as expected

As a newcomer, I am delving into the creation of a memory game. The main objective is to fetch data from an API and filter it to only include items with image links. On level one of the game, the task is to display three random images from the fetched data ...

In order for Angular forms to be considered valid, they must fall into one of two form

I am attempting to create a dynamic angular form where the user must enter either A or B. A represents a unique identifier, while B consists of a set of values that correspond to that identifier. My goal is to validate the form as valid if either A is ente ...