我在gridview中有这两个文本框的项目模板,我试图在它们之间进行计算但是在网格内部没有工作。这是我的代码
<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 :(得分:3)
只更改aspx.cs页面“onkeyup” txtFATObj.Attributes.Add(“onkeyup”,“Calculate('”txtFATObj.ClientID +“','”+ txtSNFObj.ClientID +“','”+ txtNETWTObj.ClientID +“','”+ txtTSObj.ClientID + “')”);
答案 1 :(得分: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 + "')");
}
}
答案 2 :(得分:0)
这应该有效:
onkeydown="document.getElementById('<%= txtRate.ClientID %>').value=this.value;"