請檢查下面的代碼。注意你需要調整..整數或浮動..等我測試了一些基於你的公式的計算。
- 擺在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 + "')");
}
}
什麼是您的計算公式?你有兩個文本框,然後計算過程是? – 2012-03-01 07:20:53
txtTS = txtFAT + txtSNF * txtNETWT/14這是公式 – 2012-03-01 07:28:21
我已經更新了我的所有列在我的問題 – 2012-03-01 07:29:59