ASPxGridView如何在编辑模式下将两个字段相乘并显示在第三个字段中

时间:2015-03-03 11:12:01

标签: asp.net field aspxgridview

我已经在ASPxGridView中加载数据,当我点击编辑链接时,数据显示在编辑模式(PopupForm模式)。 当用户更改数量字段值时,在数量字段值与价格字段值相乘后,将更新金额字段。 金额将乘以价值。在此之后用户将点击更新链接,数据将在数据库中修改。

这是我编写的ASP.NET代码

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EnableTheming="True" KeyFieldName="transaction_id" OnRowCommand="ASPxGridView1_RowCommand" OnRowUpdated="ASPxGridView1_RowUpdated" OnRowUpdating="ASPxGridView1_RowUpdating" Theme="Metropolis">
                    <Columns>
                        <dx:GridViewCommandColumn ShowDeleteButton="True" ShowEditButton="True" VisibleIndex="0">
                        </dx:GridViewCommandColumn>
                        <dx:GridViewDataTextColumn FieldName="item_number" VisibleIndex="1">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="item_description" VisibleIndex="2">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="item_unit" VisibleIndex="3">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="item_quantity" VisibleIndex="4">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="item_price" VisibleIndex="5">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="item_amount" VisibleIndex="6">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="voucher_number" VisibleIndex="7">
                        </dx:GridViewDataTextColumn>
                    </Columns>
                    <SettingsBehavior ConfirmDelete="True" />
                    <SettingsEditing Mode="PopupEditForm">
                    </SettingsEditing>
                    <Settings ShowFilterRow="True" ShowFooter="True" />
                </dx:ASPxGridView>
            </ContentTemplate>
        </asp:UpdatePanel>

        <br />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:item_detailConnectionString %>" DeleteCommand="DELETE FROM [tbl_item_detail_transactions] WHERE [transaction_id] = @transaction_id" InsertCommand="INSERT INTO [tbl_item_detail_transactions] ([item_number], [item_description], [item_unit], [item_quantity], [item_price], [item_amount], [voucher_number]) VALUES (@item_number, @item_description, @item_unit, @item_quantity, @item_price, @item_amount, @voucher_number)" SelectCommand="SELECT  *, (item_quantity * item_price) as item_amount2 FROM [tbl_item_detail_transactions]" UpdateCommand="UPDATE [tbl_item_detail_transactions] SET [item_number] = @item_number, [item_description] = @item_description, [item_unit] = @item_unit, [item_quantity] = @item_quantity, [item_price] = @item_price, [item_amount] = @item_amount, [voucher_number] = @voucher_number WHERE [transaction_id] = @transaction_id">
            <DeleteParameters>
                <asp:Parameter Name="transaction_id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="item_number" Type="Int32" />
                <asp:Parameter Name="item_description" Type="String" />
                <asp:Parameter Name="item_unit" Type="Int32" />
                <asp:Parameter Name="item_quantity" Type="Int32" />
                <asp:Parameter Name="item_price" Type="Int32" />
                <asp:Parameter Name="item_amount2" Type="Int32" />
                <asp:Parameter Name="voucher_number" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="item_number" Type="Int32" />
                <asp:Parameter Name="item_description" Type="String" />
                <asp:Parameter Name="item_unit" Type="Int32" />
                <asp:Parameter Name="item_quantity" Type="Int32" />
                <asp:Parameter Name="item_price" Type="Int32" />
                <asp:Parameter Name="item_amount" Type="Int32" />
                <asp:Parameter Name="voucher_number" Type="Int32" />
                <asp:Parameter Name="transaction_id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>

这是一个JavaScript代码

 <script src="Scripts/jquery-1.8.2.js"></script>
<script src="Scripts/jquery-1.4.1.min.js"></script>

 <script type="text/javascript">
    $(function () {
        $("#ASPxGridView1").find("input[title='item_quantity']").blur(function () {
            var item_quantity = $(this).val();
            var item_price = $("#ASPxGridView1").find("input[title='item_price']").val();
            if (item_quantity != "" && item_price != "") {
                var total = parseInt(item_price) + parseInt(item_quantity);
                $("#ASPxGridView1").find("input[title='item_amount']").val(total);
            } else {
                $("#ASPxGridView1").find("input[title='item_amount']").val("");
            }
        })
    })
</script>

1 个答案:

答案 0 :(得分:0)

您可以参考以下示例,在键入特定字段时自动在编辑模式下填充TextBox。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../../../Scripts/jquery-1.8.2.js"></script>
<script>
    $(function () {
        $("#GridView1").find("input[title='Val2']").blur(function () {
            var val2 = $(this).val();
            var val1 = $("#GridView1").find("input[title='Val1']").val();
            if (val2 != "" && val1 != "") {
                var total = parseInt(val1) + parseInt(val2);
                $("#GridView1").find("input[title='Total']").val(total);
            } else {
                $("#GridView1").find("input[title='Total']").val("");
            }
        })
    })
</script>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="Val1" HeaderText="Val1" SortExpression="Val1" />
                <asp:BoundField DataField="Val2" HeaderText="Val2" SortExpression="Val2" />
                <asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [CalTotal] WHERE [Id] = @original_Id AND (([Val1] = @original_Val1) OR ([Val1] IS NULL AND @original_Val1 IS NULL)) AND (([Val2] = @original_Val2) OR ([Val2] IS NULL AND @original_Val2 IS NULL)) AND (([Total] = @original_Total) OR ([Total] IS NULL AND @original_Total IS NULL))" InsertCommand="INSERT INTO [CalTotal] ([Val1], [Val2], [Total]) VALUES (@Val1, @Val2, @Total)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [CalTotal]" UpdateCommand="UPDATE [CalTotal] SET [Val1] = @Val1, [Val2] = @Val2, [Total] = @Total WHERE [Id] = @original_Id AND (([Val1] = @original_Val1) OR ([Val1] IS NULL AND @original_Val1 IS NULL)) AND (([Val2] = @original_Val2) OR ([Val2] IS NULL AND @original_Val2 IS NULL)) AND (([Total] = @original_Total) OR ([Total] IS NULL AND @original_Total IS NULL))">
            <DeleteParameters>
                <asp:Parameter Name="original_Id" Type="Int32" />
                <asp:Parameter Name="original_Val1" Type="Int32" />
                <asp:Parameter Name="original_Val2" Type="Int32" />
                <asp:Parameter Name="original_Total" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Val1" Type="Int32" />
                <asp:Parameter Name="Val2" Type="Int32" />
                <asp:Parameter Name="Total" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Val1" Type="Int32" />
                <asp:Parameter Name="Val2" Type="Int32" />
                <asp:Parameter Name="Total" Type="Int32" />
                <asp:Parameter Name="original_Id" Type="Int32" />
                <asp:Parameter Name="original_Val1" Type="Int32" />
                <asp:Parameter Name="original_Val2" Type="Int32" />
                <asp:Parameter Name="original_Total" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </div>
</form>
</body>
</html>