Gridview编辑时Jquery自动计算自定义列(鼠标离开输入框Jquery计算)
Posted on 2015-09-18 17:25 木头人Ricky 阅读(270) 评论(0) 编辑 收藏 举报Jquery片段:
<script type="text/javascript"> function compute(nbBoxQuantity, lblQuantityPerBox, lblQuantity, nbPrice, lblTotalAmounts) { if (nbBoxQuantity.value == "" || nbBoxQuantity.length == 0) { nbBoxQuantity.value = 0; } if (lblQuantityPerBox.innerText == "" || lblQuantityPerBox.length == 0) { lblQuantityPerBox.innerText(0); } if (nbPrice.value == "" || nbPrice.length == 0) { nbPrice.value = 0; } if (lblQuantity.innerText == "" || lblQuantity.length == 0) { lblQuantity.innerText(0); } lblQuantity.innerText = nbBoxQuantity.value * lblQuantityPerBox.innerText; lblTotalAmounts.innerText = nbPrice.value * lblQuantity.innerText; } </script>
前台片段:
<swc:DataGridView ID="dgvList" runat="server" DataKeyNames="ID" AllowSorting="true" CurrentSortField="GoodsId" OnRowDeleting="dgvList_RowDeleting" OnRowEditing="dgvList_RowEditing" OnRowDataBound="dgvList_RowDataBound" OnRowCommand="dgvList_RowCommand" OnRowCancelingEdit="dgvList_RowCancelingEdit" OnRowUpdating="dgvList_RowUpdating"> <Columns> <swc:TemplateField HeaderText="Id" Visible="false"> <ItemTemplate> <asp:Literal runat="server" ID="ltlId"></asp:Literal> </ItemTemplate> <EditItemTemplate> <asp:Literal runat="server" ID="ltlId"></asp:Literal> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="编号" Width="100" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Literal runat="server" ID="ltlGoodsCode"></asp:Literal> </ItemTemplate> <EditItemTemplate> <asp:Literal runat="server" ID="ltlGoodsCode"></asp:Literal> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="名称" Width="100" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Literal runat="server" ID="ltlGoodsName"></asp:Literal> </ItemTemplate> <EditItemTemplate> <asp:Literal runat="server" ID="ltlGoodsName"></asp:Literal> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="类型" Width="60" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Literal ID="ltlType" runat="server" /> </ItemTemplate> <EditItemTemplate> <asp:Literal ID="ltlType" runat="server" /> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="赠品" Width="60" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Literal ID="ltlIsGift" runat="server" /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlIsGift" runat="server"> <asp:ListItem Value="True">是</asp:ListItem> <asp:ListItem Value="False">否</asp:ListItem> </asp:DropDownList> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="箱数" Width="60" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Literal ID="ltlBoxQuantity" runat="server" /> </ItemTemplate> <EditItemTemplate> <swc:NumberBox ID="nbBoxQuantity" runat="server" Digit="0" Width="60" /> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="每箱数" Width="60" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="lblQuantityPerBox" runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblQuantityPerBox" runat="server"></asp:Label> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="数量" Width="60" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="lblQuantity" runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblQuantity" runat="server"></asp:Label> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="单价" Width="60" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Literal ID="ltlPrice" runat="server" /> </ItemTemplate> <EditItemTemplate> <swc:NumberBox ID="nbPrice" runat="server" Digit="2" Width="60" /> </EditItemTemplate> </swc:TemplateField> <swc:TemplateField HeaderText="总价" Width="60" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="lblTotalAmounts" runat="server"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblTotalAmounts" runat="server"></asp:Label> </EditItemTemplate> </swc:TemplateField> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" HeaderStyle-Width="60" /> </Columns> </swc:DataGridView>
后台片段:
protected void dgvList_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; switch (row.RowType) { case DataControlRowType.DataRow: SC_PurchaseItem item = row.DataItem as SC_PurchaseItem; if (item.Id != Guid.Empty) { Literal ltlId = row.FindControl<Literal>("ltlId"); Literal ltlGoodsCode = row.FindControl<Literal>("ltlGoodsCode"); Literal ltlGoodsName = row.FindControl<Literal>("ltlGoodsName"); Literal ltlType = row.FindControl<Literal>("ltlType"); Label lblQuantityPerBox = row.FindControl<Label>("lblQuantityPerBox"); Label lblQuantity = row.FindControl<Label>("lblQuantity"); Label lblTotalAmounts = row.FindControl<Label>("lblTotalAmounts"); var good = bll.Get<M_Goods>(item.GoodsId); ltlId.Text = item.Id.ToString(); ltlGoodsCode.Text = good.GoodsCode; ltlGoodsName.Text = good.GoodsName; ltlType.Text = good.GoodsType.ToText(); lblQuantityPerBox.Text = item.QuantityPerBox.ToString(); //if (e.Row.RowState == DataControlRowState.Edit) if (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit) || e.Row.RowState == DataControlRowState.Edit) { System.Web.UI.WebControls.DropDownList ddlIsGift = row.FindControl<System.Web.UI.WebControls.DropDownList>("ddlIsGift"); NumberBox nbBoxQuantity = row.FindControl<NumberBox>("nbBoxQuantity"); NumberBox nbPrice = row.FindControl<NumberBox>("nbPrice"); ddlIsGift.SelectedValue = item.IsGift.ToString(); nbBoxQuantity.Value = item.BoxQuantity.ToHtmlDecode(); nbPrice.Text = item.Price.ToHtmlDecode(); nbBoxQuantity.Attributes.Add("onchange", "compute(" + nbBoxQuantity.ClientID + "," + lblQuantityPerBox.ClientID + "," + lblQuantity.ClientID + "," + nbPrice.ClientID + "," + lblTotalAmounts.ClientID + ");"); nbPrice.Attributes.Add("onchange", "compute(" + nbBoxQuantity.ClientID + "," + lblQuantityPerBox.ClientID + "," + lblQuantity.ClientID + "," + nbPrice.ClientID + "," + lblTotalAmounts.ClientID + ");"); } else { Literal ltlIsGift = row.FindControl<Literal>("ltlIsGift"); Literal ltlBoxQuantity = row.FindControl<Literal>("ltlBoxQuantity"); Literal ltlPrice = row.FindControl<Literal>("ltlPrice"); ltlIsGift.Text = item.IsGift ? "是" : "否"; ltlBoxQuantity.Text = item.BoxQuantity.ToHtmlDecode(); ltlPrice.Text = item.Price.ToHtmlDecode(); } lblQuantity.Text = (item.BoxQuantity * item.QuantityPerBox).ToHtmlDecode(); lblTotalAmounts.Text = ((item.BoxQuantity * item.QuantityPerBox) * item.Price).ToHtmlDecode(); } break; } } protected void dgvList_RowCommand(object sender, GridViewCommandEventArgs e) { } protected void dgvList_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridView gv = (GridView)sender; string id = ((System.Web.UI.WebControls.Literal)gv.Rows[e.RowIndex].FindControl("ltlId")).Text; SC_PurchaseItem entity = new SC_PurchaseItem { Id = new Guid(id), UpdatedDate = DateTime.Now, UpdatedUser = CurrentUser.DisplayName }; PurchaseBll bll = new PurchaseBll(); bll.DeletePurchaseItem(entity); this.BindGrid(); } protected void dgvList_RowEditing(object sender, GridViewEditEventArgs e) { GridView gv = (GridView)sender; gv.EditIndex = e.NewEditIndex; this.BindGrid(); } protected void dgvList_Sorting(object sender, GridViewSortEventArgs e) { this.BindGrid(); } protected void dgvList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { dgvList.EditIndex = -1; this.BindGrid(); } protected void dgvList_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridView gv = (GridView)sender; string id = ((System.Web.UI.WebControls.Literal)gv.Rows[e.RowIndex].FindControl("ltlId")).Text; bool ddlIsGift = ((System.Web.UI.WebControls.DropDownList)gv.Rows[e.RowIndex].FindControl("ddlIsGift")).SelectedValue.As<bool>(); int nbBoxQuantity = ((NumberBox)gv.Rows[e.RowIndex].FindControl("nbBoxQuantity")).Value.As<int>(); int lblQuantity = ((Label)gv.Rows[e.RowIndex].FindControl("lblQuantity")).Text.As<int>(); decimal nbPrice = ((NumberBox)gv.Rows[e.RowIndex].FindControl("nbPrice")).Value.As<decimal>(); PurchaseBll bll = new PurchaseBll(); SC_PurchaseItem entity = new SC_PurchaseItem { Id = new Guid(id), IsGift = ddlIsGift, BoxQuantity = nbBoxQuantity, Quantity = lblQuantity, Price = nbPrice, UpdatedDate = DateTime.Now, UpdatedUser = CurrentUser.DisplayName }; bll.UpdatePurchaseItem(entity); gv.EditIndex = -1; this.BindGrid(); }