当前位置: 代码迷 >> ASP.NET >> 100分!gridview编辑状态下有4个文本框··要自动计算前3个文本框中float数据类型的值的积··求实现JS代码··该如何处理
  详细解决方案

100分!gridview编辑状态下有4个文本框··要自动计算前3个文本框中float数据类型的值的积··求实现JS代码··该如何处理

热度:2650   发布时间:2013-02-25 00:00:00.0
100分!gridview编辑状态下有4个文本框··要自动计算前3个文本框中float数据类型的值的积··求实现JS代码··
gridview编辑状态下有4个文本框··要自动计算前3个文本框中float数据类型的值的积··求实现JS代码··

我自己写的JS但不行
function jsh(obj1,obj2,obj3,obj4)  
  {  
  var value1=obj1.value;  
  var value2=obj2.value;  
  var value3=obj3.value; 
  var value4=obj4.value;  
  if(document.all.TextBox5.value!=0)
  {
  value1=(document.all.TextBox5.value);  
   
  } else
  {
  value1=0;  
   
  }  
  if(document.all.TextBox8.value!=0)
  {
  value2=(document.all.TextBox8.value);  
   
  } else
  {
  value2=0;  
   
  }  
  if(document.all.TextBox11.value!=0)
  {
  value3=(document.all.TextBox11.value);  
   
  } else
  {
  value3=0;  
   
  }  
  if(document.all.TextBox14.value!=0)
  {
  value4=(document.all.TextBox14.value);  
   
  } else
  {
  value4=0;  
   
  }  
   
  document.all.TextBox33.value=parseFloat(value1)+parseFloat(value2)+parseFloat(value3)+parseFloat(value4)
  }  

gridview代码··
<asp:TemplateField HeaderText="用量(Yid)" SortExpression="yid">
  <EditItemTemplate>
  <asp:TextBox ID="TextBox4" runat="server" Height="22px" 
  Text='<%# Bind("yid") %>' Width="50px" onkeyup="buliao(this,this,this,TextBox6)"></asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
  <asp:Label ID="Label4" runat="server" Text='<%# Bind("yid") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="单价(price)" SortExpression="price">
  <EditItemTemplate>
  <asp:TextBox ID="TextBox5" runat="server" Height="22px" 
  Text='<%# Bind("price") %>' Width="50px" onkeyup="buliao(this,this,this,TextBox6)"></asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
  <asp:Label ID="Label5" runat="server" Text='<%# Bind("price") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="价格(Total)" SortExpression="total">
  <EditItemTemplate>
  <asp:TextBox ID="TextBox6" runat="server" Height="22px" 
  Text='<%# Bind("total") %>' Width="50px"></asp:TextBox>
  </EditItemTemplate>
  <ItemTemplate>
  <asp:Label ID="Label6" runat="server" Text='<%# Bind("total") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="损耗(Wastage)" SortExpression="wastage">
  <EditItemTemplate>
  <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
  onselectedindexchanged="DropDownList2_SelectedIndexChanged1" onkeyup="buliao(this,this,this,TextBox6)">
  <asp:ListItem>1.01</asp:ListItem>
  <asp:ListItem>1.02</asp:ListItem>
  <asp:ListItem>1.03</asp:ListItem>
  <asp:ListItem>1.04</asp:ListItem>
  <asp:ListItem>1.05</asp:ListItem>
  <asp:ListItem>1.06</asp:ListItem>
  <asp:ListItem>1.07</asp:ListItem>
  <asp:ListItem>1.08</asp:ListItem>
  <asp:ListItem>1.09</asp:ListItem>
  <asp:ListItem>1.10</asp:ListItem>
  </asp:DropDownList>
  </EditItemTemplate>
  <ItemTemplate>
  <asp:Label ID="Label7" runat="server" Text='<%# Bind("wastage") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="总价(TOTAL)" SortExpression="TOTALPRICE">
  <EditItemTemplate>
  <asp:TextBox ID="TextBox8" runat="server" Height="22px" 
  Text='<%# Bind("TOTALPRICE") %>' Width="50px"></asp:TextBox>
  </EditItemTemplate>

------解决方案--------------------------------------------------------
HTML code
<asp:TemplateField HeaderText="用量(Yid)" SortExpression="yid">                             <EditItemTemplate>                                 <asp:TextBox [color=#FF0000]ID="TextBox4" onkeydown="checkFloat(event)" runat="server" Height="22px"                                     Text=' <%# Bind("yid") %>' Width="50px" onkeyup="buliao(this)"> </asp:TextBox> [/color]                            </EditItemTemplate>                             <ItemTemplate>                                 <asp:Label ID="Label4" runat="server" Text=' <%# Bind("yid") %>'> </asp:Label>                             </ItemTemplate>                         </asp:TemplateField>                         <asp:TemplateField HeaderText="单价(price)" SortExpression="price">                             <EditItemTemplate>                                 <asp:TextBox ID="TextBox5" onkeydown="checkFloat(event)" runat="server" Height="22px"                                     Text=' <%# Bind("price") %>' Width="50px" onkeyup="buliao(this)"> </asp:TextBox>                             </EditItemTemplate>                             <ItemTemplate>                                 <asp:Label ID="Label5" runat="server" Text=' <%# Bind("price") %>'> </asp:Label>                             </ItemTemplate>                         </asp:TemplateField>                         <asp:TemplateField HeaderText="价格(Total)" SortExpression="total">                             <EditItemTemplate>                                 <asp:TextBox ID="TextBox6" runat="server" Height="22px"                                     Text=' <%# Bind("total") %>' Width="50px"> </asp:TextBox>                             </EditItemTemplate>                             <ItemTemplate>                                 <asp:Label ID="Label6" runat="server" Text=' <%# Bind("total") %>'> </asp:Label>                             </ItemTemplate>                         </asp:TemplateField>                         <asp:TemplateField HeaderText="损耗(Wastage)" SortExpression="wastage">                             <EditItemTemplate>                                 <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"                                     onselectedindexchanged="DropDownList2_SelectedIndexChanged1" onkeyup="buliao(this,this,this,TextBox6)">                                     <asp:ListItem>1.01 </asp:ListItem>                                     <asp:ListItem>1.02 </asp:ListItem>                                     <asp:ListItem>1.03 </asp:ListItem>                                     <asp:ListItem>1.04 </asp:ListItem>                                     <asp:ListItem>1.05 </asp:ListItem>                                     <asp:ListItem>1.06 </asp:ListItem>                                     <asp:ListItem>1.07 </asp:ListItem>                                     <asp:ListItem>1.08 </asp:ListItem>                                     <asp:ListItem>1.09 </asp:ListItem>                                     <asp:ListItem>1.10 </asp:ListItem>                                 </asp:DropDownList>                             </EditItemTemplate>                             <ItemTemplate>                                 <asp:Label ID="Label7" runat="server" Text=' <%# Bind("wastage") %>'> </asp:Label>                             </ItemTemplate>                         </asp:TemplateField>                         <asp:TemplateField HeaderText="总价(TOTAL)" SortExpression="TOTALPRICE">                             <EditItemTemplate>                                 <asp:TextBox ID="TextBox8" runat="server" Height="22px"                                     Text=' <%# Bind("TOTALPRICE") %>' Width="50px"> </asp:TextBox>                             </EditItemTemplate>
------解决方案--------------------------------------------------------
如果计算发生在第4个文本框获取焦点的时候:
<input id=text4 type=text onfocus="getNum(this)"/>

function getNum(layer)
{
var tr = layer.parentNode.parentNode;//获取text4所在行
var n1 = tr.childNodes[0].childNodes[0].value;
var n2 = tr.childNodes[1].childNodes[0].value;
var n3 = tr.childNodes[2].childNodes[0].value;
var n4 = parseInt(n1)*parseInt(n2)*parseInt(n3);//最好先判断一下是否为int:if(isNaN(n1))alert("非数字");
layer.value = n4;
}
------解决方案--------------------------------------------------------
http://blog.csdn.net/antiking/archive/2008/11/11/3278059.aspx
------解决方案--------------------------------------------------------
一下测试全部通过的。楼主参考一下。
里面调用的buliao方法我都把参数去掉了,用不着带参数。"损耗"那里我改成onclick事件,不是用onkeyup事件。
我加了一个文本框txtGridViewRow,用来保存编辑行索引.在触发Gridview1_RowEditing事件时给它赋值
C# code
protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e){   Gridview1.EditIndex = e.NewEditIndex;   txtGridViewRow.Text = e.NewEditIndex.ToString();   BindGrid();}页面:<asp:TextBox runat="server" ID="txtGridViewRow"></asp:TextBox>GridView:       <asp:TemplateField HeaderText="用量(Yid)" SortExpression="yid">                     <EditItemTemplate>                        <asp:TextBox ID="TextBox4" runat="server" Height="22px" Text=' <%# Bind("yid") %>' Width="50px" onkeyup="javascript:buliao();"> </asp:TextBox>                     </EditItemTemplate>                     <ItemTemplate>                         <asp:Label ID="Label4" runat="server" Text=' <%# Bind("yid") %>'> </asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                 <asp:TemplateField HeaderText="单价(price)" SortExpression="price">                     <EditItemTemplate>                         <asp:TextBox ID="TextBox5" runat="server" Height="22px"                             Text=' <%# Bind("price") %>' Width="50px" onkeyup="javascript:buliao();"> </asp:TextBox>                    </EditItemTemplate>                     <ItemTemplate>                         <asp:Label ID="Label5" runat="server" Text=' <%# Bind("price") %>'> </asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                 <asp:TemplateField HeaderText="价格(Total)" SortExpression="total">                     <EditItemTemplate>                         <asp:TextBox ID="TextBox6" runat="server" Height="22px"                             Text=' <%# Bind("total") %>' Width="50px"> </asp:TextBox>                     </EditItemTemplate>                    <ItemTemplate>                         <asp:Label ID="Label6" runat="server" Text=' <%# Bind("total") %>'> </asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                 <asp:TemplateField HeaderText="损耗(Wastage)" SortExpression="wastage">                     <EditItemTemplate>                         <asp:DropDownList ID="DropDownList2" runat="server" onclick="javascript:buliao();">                            <asp:ListItem>1.01 </asp:ListItem>                             <asp:ListItem>1.02 </asp:ListItem>                             <asp:ListItem>1.03 </asp:ListItem>                             <asp:ListItem>1.04 </asp:ListItem>                             <asp:ListItem>1.05 </asp:ListItem>                             <asp:ListItem>1.06 </asp:ListItem>                             <asp:ListItem>1.07 </asp:ListItem>                             <asp:ListItem>1.08 </asp:ListItem>                             <asp:ListItem>1.09 </asp:ListItem>                             <asp:ListItem>1.10 </asp:ListItem>                         </asp:DropDownList>                     </EditItemTemplate>                     <ItemTemplate>                         <asp:Label ID="Label7" runat="server" Text='d'> </asp:Label>                     </ItemTemplate>                 </asp:TemplateField>
  相关解决方案