How to assign and retrieve value from the GridView Cell using JavaScript?
I have Two Labels such as FixedTotal and TotalPercent, One GridView which has One Row such as
ReferenceID Percentage Amount
-------------- ------------- --------
开发者_运维知识库 1 5 1000
Here I want to change the "Percentage" Column value 5 with the TotalPercent label's text. and I want to display the "Amount" Column value 1000 in the FixedTotal label. I also want to check whether the Grid has row or not? How to do this? The Columns are BoundField Columns..
Getting/Setting
value to gridview cells, you need to know the Row Index
of Gridview, you can pass row index
when you call JS function from 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
...........
.............
}
<script language="javascript" type="text/javascript">
function Calculate()
<br/>
{
<br/>
var grid = document.getElementById("<%=GridID.ClientID%>");
<br/>
var sum = 0; <br/>
for (var i = 1; i < grid.rows.length; i++)<br/>
{ <br/>
var Cell = grid.rows[i].getElementsByTagName("input");
<br/>if (!Cell[4].value) {sum += 0; } else { sum += parseFloat(Cell[4].value);} }
<br/>
document.getElementById("<%=TextBox1.ClientID%>").value = sum;
}
<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>
You can try this one. I have used this and has no error.
<script language="javascript" type="text/javascript">
function update(rowIndexOfGridview) {
var ri = rowIndexOfGridview;
var grd = document.getElementById('<%= GridView1.ClientID %>');
var CellValue = grd.rows[ri].cells[1].childNodes[0].textContent); // get
grd.rows[ri].cells[2].childNodes[0].textContent = CellValue; //assign
}
</script>
精彩评论