2012-03-01 85 views
0

我GridView的文本框裏面的這兩個的ItemTemplate,我試圖讓他們之間的計算,但不grid.here內工作是我的代碼化妝計算

<asp:TemplateField HeaderText="Net Weight"> 
        <ItemTemplate> 
         <asp:TextBox ID="txtNetWT" runat="server" Width="70px" AutoPostBack=false></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Rate"> 
        <ItemTemplate> 
         <asp:TextBox ID="txtRate" runat="server" Width="70px" ></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="SNF"> 
        <ItemTemplate> 
         <asp:TextBox ID="txtSNF" runat="server" Width="70px" ></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="FAT"> 
        <ItemTemplate> 
         <asp:TextBox ID="txtFat" runat="server" Width="70px" ></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="LR"> 
        <ItemTemplate> 
         <asp:TextBox ID="txtLR" runat="server" Width="70px"></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="14 TS"> 
        <ItemTemplate> 
         <asp:TextBox ID="txtTS" runat="server" Width="70px" ></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 

但是當我進入這個值對其他元素沒有影響。請告訴我在Javascript中的任何適當的功能來做到這一點。

+0

什麼是您的計算公式?你有兩個文本框,然後計算過程是? – 2012-03-01 07:20:53

+0

txtTS = txtFAT + txtSNF * txtNETWT/14這是公式 – 2012-03-01 07:28:21

+0

我已經更新了我的所有列在我的問題 – 2012-03-01 07:29:59

回答

2

請檢查下面的代碼。注意你需要調整..整數或浮動..等我測試了一些基於你的公式的計算。

- 擺在JavaScript部分中頭

<script type="text/javascript" language="javascript"> 
     function Calculate(txtFAT, txtSNF, txtNETWT, txtTS) { 
      var txtFATObj = document.getElementById(txtFAT); 
      var txtSNFObj = document.getElementById(txtSNF); 
      var txtNETWTObj = document.getElementById(txtNETWT); 
      var txtTSObj = document.getElementById(txtTS); 

      if (txtFATObj != null && txtSNFObj != null && txtNETWTObj != null && txtTSObj != null) { 
       txtTSObj.value = parseFloat(txtFATObj.value) + (parseFloat(txtSNFObj.value) * parseFloat(txtNETWTObj.value)/14); 
      } 
     } 
    </script> 

---測試GridView控件代碼ASPX -----------

<asp:GridView ID="grvCalc" runat="server" AutoGenerateColumns="false" 
     onrowdatabound="grvCalc_RowDataBound"> 
     <Columns> 
      <asp:BoundField HeaderText="Item" DataField="Item" /> 
      <asp:TemplateField HeaderText="Net Weight"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtNetWT" runat="server" Width="70px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Rate"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtRate" runat="server" Width="70px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="SNF"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtSNF" runat="server" Width="70px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="FAT"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtFat" runat="server" Width="70px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="LR"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtLR" runat="server" Width="70px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="14 TS"> 
       <ItemTemplate> 
        <asp:TextBox ID="txtTS" runat="server" Width="70px"></asp:TextBox> 
       </ItemTemplate> 
      </asp:TemplateField>    
     </Columns> 
    </asp:GridView> 

----- ----測試數據綁定到gridview的----------------

private void BindGridView() 
     { 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("Item"); 

      DataRow dr = dt.NewRow(); 
      dr[0] = "Item 1"; 
      dt.Rows.Add(dr); 

      dr = dt.NewRow(); 
      dr[0] = "Item 2"; 
      dt.Rows.Add(dr); 

      dr = dt.NewRow(); 
      dr[0] = "Item 3"; 
      dt.Rows.Add(dr); 

      grvCalc.DataSource = dt; 
      grvCalc.DataBind(); 
     } 

-----需要附加每個文本控制的客戶端事件用於測算參與 - gridvew行數據綁定事件..計算將執行onfoucus文本框的事件

protected void grvCalc_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       TextBox txtFATObj = (TextBox)e.Row.FindControl("txtFAT"); 
       TextBox txtSNFObj = (TextBox)e.Row.FindControl("txtSNF"); 
       TextBox txtNETWTObj = (TextBox)e.Row.FindControl("txtNETWT"); 
       TextBox txtTSObj = (TextBox)e.Row.FindControl("txtTS"); 

       txtFATObj.Attributes.Add("onfocusout", "Calculate('" + txtFATObj.ClientID + "','" + txtSNFObj.ClientID + "','" + txtNETWTObj.ClientID + "','" + txtTSObj .ClientID + "')"); 
       txtSNFObj.Attributes.Add("onfocusout", "Calculate('" + txtFATObj.ClientID + "','" + txtSNFObj.ClientID + "','" + txtNETWTObj.ClientID + "','" + txtTSObj.ClientID + "')"); 
       txtNETWTObj.Attributes.Add("onfocusout", "Calculate('" + txtFATObj.ClientID + "','" + txtSNFObj.ClientID + "','" + txtNETWTObj.ClientID + "','" + txtTSObj.ClientID + "')"); 
      } 
     } 
+0

在哪裏調用Calculate(txtFAT,txtSNF,txtNETWT,txtTS)網格視圖 – 2012-03-01 09:19:10

+0

裏面的GridView行數據綁定事件..我已經porivded的代碼。 – 2012-03-01 09:21:35

0

這應該工作:

onkeydown="document.getElementById('<%= txtRate.ClientID %>').value=this.value;" 
+0

沒有兄弟它什麼都沒有給其他文本框 – 2012-03-01 07:27:03

3

只有改變aspx.cs頁面 「的onkeyup」 txtFATObj.Attributes.Add( 「的onkeyup」,「計算(」 「+ txtFATObj.ClientID +」','「+ txtSNFObj.ClientID +」','「+ txtNETWTObj.ClientID +」','「+ txtTSObj.ClientID +」')「);