最近做个项目,里面要在gridview 的 textbox 值改变时候做一些操作,代码如下:
<asp:GridView ID="gvDesign" Width="480px" runat="server" AutoGenerateColumns="False"
GridLines="None" BorderStyle="None" BorderWidth="0px" CellPadding="3" EnableModelValidation="True"
DataKeyNames="Id" OnRowDataBound="gvDesign_RowDataBound">
<%--<AlternatingRowStyle BackColor="#DCDCDC" />--%>
<Columns>
<asp:TemplateField ItemStyle-Width="150px" HeaderText="" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:TextBox id="txtCost" onpropertychange="CalculateProfit(this);" runat="server"/>
<asp:DropDownList ID="ddlPriceCurrency" runat="server" onChange="CalculateProfit(this);">
<asp:ListItem Value="1">USD</asp:ListItem>
<asp:ListItem Value="2">EUR</asp:ListItem>
<asp:ListItem Value="3" Selected="True">RMB</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
以下是js 代码:
function CalculateProfit(obj) {
//CurrencyRates send from server side as an Array.
//alert(obj.parentNode.parentNode.cells[1].childNodes[1].value);
//debugger;
var strCost = obj.parentNode.parentNode.cells[0].childNodes[0].value;
var originalCost = strCost.toString().replace(/[^\d]/g, "");
}
结果发现obj.parentNode.parentNode.cells[0].value的值为空,需要把cells[0]改为cells[1],但是发布到IIS里面是能取到值的,后来反复研究 ,各种调试,也没个解决方案,这真是一个奇葩的问题,最后直接替换成obj.parentNode.parentNode.cells[0].getElementsByTagName("input")[0].value这样就不会纠结于那个node了,至此问题解决。