如何使用 JavaScript 从 GridView Cell 中分配和检索值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6293006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:12:13  来源:igfitidea点击:

How to assign and retrieve value from the GridView Cell using JavaScript?

javascript.netasp.netgridview

提问by thevan

I have Two Labels such as FixedTotal and TotalPercent, One GridView which has One Row such as

我有两个标签,例如 FixedTotal 和 TotalPercent,一个 GridView 有一行,例如

    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..

在这里,我想使用 TotalPercent 标签的文本更改“百分比”列值 5。我想在 FixedTotal 标签中显示“金额”列值 1000。我还想检查网格是否有行?这个怎么做?列是 BoundField 列..

回答by Muhammad Akhtar

Getting/Settingvalue to gridview cells, you need to know the Row Indexof Gridview, you can pass row indexwhen you call JS function from gridview

Getting/Settinggridview单元格的值,你需要知道Row IndexGridview的,pass row index当你从gridview调用JS函数时可以

<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
    ...........
    .............
}

回答by Anuradha Jayalath

<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>