0

我在gridview中有这两个文本框的itemtemplates,我试图在它们之间进行计算,但在grid中不起作用。这是我的代码

  <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 中的任何适当的函数来执行此操作。

4

3 回答 3

3

只改变aspx.cs页面 "onkeyup" txtFATObj.Attributes.Add("onkeyup", "Calculate('" + txtFATObj.ClientID + "','" + txtSNFObj.ClientID + "','" + txtNETWTObj.ClientID + "','" + txtTSObj.ClientID + "')");

于 2012-12-10T06:45:04.220 回答
2

请检查以下代码。请注意,您需要调整..整数或浮点数..等。我根据您的公式测试了一些计算。

-- 将 javascript 部分放入 head 中

<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行数据绑定事件..计算将执行文本框的焦点事件

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-01T08:38:59.830 回答
0

这应该有效:

onkeydown="document.getElementById('<%= txtRate.ClientID %>').value=this.value;"
于 2012-03-01T07:20:26.683 回答