在更新面板代码之后,某些计算值无法设置到文本框中。

时间:2018-10-19 16:55:25

标签: c# asp.net

在此代码中,此文本更改方法是从更新面板触发器中调用的,我执行一些计算(即将gridview单元格值相加)并将计算结果存储在总变量中。然后将此总变量分配给txtTotalAmount文本框,但是成功执行代码后,此值在该文本框中不可见。

要获得帮助,请先感谢:

protected void txtProductPrice_TextChanged(object sender, EventArgs)
        {
            DataTable dt;
            DataRow dr = null;
            if (ViewState["CurrentData"] != null)
            {
                dt = (DataTable)ViewState["CurrentData"];
                dr = dt.NewRow();

                dr[0] = ddlProductName.SelectedItem.Value;
                dr[1] = ddlProductName.SelectedItem.ToString();
                dr[2] = txtQuantity.Text;
                dr[3] = txtUnit.Text;
                dr[4] = txtProductPrice.Text;
                dr[5] = Convert.ToDouble(txtProductPrice.Text) * Convert.ToDouble(txtQuantity.Text);

                dt.Rows.Add(dr);
            }
            else
            {
                dt = new DataTable();

                dt.Columns.Add(new DataColumn("prcode", typeof(string)));//for TextBox value   
                dt.Columns.Add(new DataColumn("prname", typeof(string)));//for TextBox value   
                dt.Columns.Add(new DataColumn("qty", typeof(string)));//for TextBox value   
                dt.Columns.Add(new DataColumn("unit", typeof(string)));//for TextBox value   
                dt.Columns.Add(new DataColumn("price", typeof(string)));//for TextBox value   
                dt.Columns.Add(new DataColumn("total", typeof(string)));//for TextBox value   

                dr = dt.NewRow();
                dr[0] = ddlProductName.SelectedItem.Value;
                dr[1] = ddlProductName.SelectedItem.ToString();
                dr[2] = txtQuantity.Text;
                dr[3] = txtUnit.Text;
                dr[4] = txtProductPrice.Text;
                dr[5] = Convert.ToDouble(txtProductPrice.Text) * Convert.ToDouble(txtQuantity.Text);

                dt.Rows.Add(dr);
            }

            grdSaleInvoice.DataSource = dt;
            grdSaleInvoice.DataBind();

            ViewState["CurrentData"] = dt;
            double total = 0;
            for (int i = 0; i < grdSaleInvoice.Rows.Count; i++)
            {
                total += Convert.ToDouble(((TextBox)grdSaleInvoice.Rows[i].Cells[4].FindControl("total")).Text);
            }
            txtTotalAmount.Text = total.ToString();
        }
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" EnableEventValidation="true" CodeBehind="SaleInvoice.aspx.cs" Inherits="POS.SaleInvoice" %>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="content-wrapper">
        <div class="container-fluid">           
            <form runat="server">
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label>Invoice No</label>
                        <asp:TextBox runat="server" ReadOnly="true" CssClass="form-control" ID="txtInvoiceNo" placeholder="Auto"></asp:TextBox>
                    </div>
                    <div class="form-group col-md-4">
                        <label>Invoice Type</label>
                        <asp:DropDownList runat="server" CssClass="form-control" ID="ddlInvoiceType" placeholder="Invoice Type">
                            <asp:ListItem Text="Invoice Type" Value="-1">Invoice Type</asp:ListItem>
                            <asp:ListItem Text="Credit" Value="1">Credit</asp:ListItem>
                            <asp:ListItem Text="Cash" Value="2">Cash</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                    <div class="form-group col-md-4">
                        <label>Customer</label>
                        <asp:DropDownList runat="server" CssClass="form-control" ID="ddlCustomer" placeholder="Customer"></asp:DropDownList>
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label>Invoice Date</label>
                        <asp:TextBox runat="server" CssClass="form-control" ID="dtInvoiceDate"></asp:TextBox>
                    </div>
                    <div class="form-group col-md-8">
                        <label>Remarks</label>
                        <asp:TextBox runat="server" CssClass="form-control" ID="txtRemarks" placeholder="Remarks"></asp:TextBox>
                    </div>
                </div>
                <div class="form-row" id="productData">
                    <div class="form-group col-md-2">
                        <label>Pr. Code</label>
                        <asp:TextBox runat="server" CssClass="form-control" EnableViewState="true" value="" AutoPostBack="true" ID="txtProductCode" placeholder="Code" OnTextChanged="txtProductCode_TextChanged"></asp:TextBox>
                    </div>
                    <div class="form-group col-md-2">
                        <label>Pr. Name</label>
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:DropDownList runat="server" CssClass="form-control" value="" ID="ddlProductName" placeholder="Name" DataTextField="product_name" DataValueField="product_id"></asp:DropDownList>
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="txtProductCode" EventName="TextChanged" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </div>
                    <div class="form-group col-md-2">
                        <label>Quantity</label>
                        <asp:TextBox runat="server" CssClass="form-control" value="" ID="txtQuantity" placeholder="Quantity"></asp:TextBox>
                    </div>
                    <div class="form-group col-md-2">
                        <label>Unit</label>
                        <asp:TextBox runat="server" CssClass="form-control" ReadOnly="true" value="" ID="txtUnit" Text="Piece"></asp:TextBox>
                    </div>
                    <div class="form-group col-md-2">
                        <label>Price</label>
                        <asp:TextBox runat="server" CssClass="form-control" value="" ID="txtProductPrice" AutoPostBack="true" placeholder="Price" OnTextChanged="txtProductPrice_TextChanged"></asp:TextBox>
                    </div>
                    <div class="form-group col-md-2">
                        <label>Total</label>
                        <asp:TextBox runat="server" ReadOnly="true" CssClass="form-control" value="" ID="txtTotal" placeholder="Total"></asp:TextBox>
                    </div>
                </div>
                <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                    <ContentTemplate>
                        <asp:GridView runat="server" ID="grdSaleInvoice" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Horizontal" ForeColor="Black" Width="100%">
                            <Columns>
                                <asp:TemplateField HeaderText="Pr. Code">
                                    <ItemTemplate>
                                        <asp:TextBox runat="server" ID="prcode" Text='<%# Bind("prcode") %>' ReadOnly="false"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Pr. Name">
                                    <ItemTemplate>
                                        <asp:TextBox runat="server" ID="prname" Text='<%# Bind("prname") %>' ReadOnly="false"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Quantity">
                                    <ItemTemplate>
                                        <asp:TextBox runat="server" ID="qty" Text='<%# Bind("qty") %>' ReadOnly="false"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Unit">
                                    <ItemTemplate>
                                        <asp:TextBox runat="server" ID="unit" Text='<%# Bind("unit") %>' ReadOnly="false"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Price">
                                    <ItemTemplate>
                                        <asp:TextBox runat="server" ID="price" Text='<%# Bind("price") %>' ReadOnly="false"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Total">
                                    <ItemTemplate>
                                        <asp:TextBox runat="server" ID="total" Text='<%# Bind("total") %>' ReadOnly="false"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Delete">
                                    <ItemTemplate>                                        
                                        <asp:LinkButton ID="btnDelete" runat="server" CssClass="btn btn-danger"><i class="material-icons" style="font-size:30px">delete</i></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                            <SortedAscendingCellStyle BackColor="#F7F7F7" />
                            <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
                            <SortedDescendingCellStyle BackColor="#E5E5E5" />
                            <SortedDescendingHeaderStyle BackColor="#242121" />
                        </asp:GridView>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="txtProductPrice" EventName="TextChanged" />
                    </Triggers>
                </asp:UpdatePanel>
                    <div class="form-row">
                        <div class="form-group col-md-4">
                            <label>Discount</label>
                            <asp:TextBox ID="txtDiscount"  CssClass="form-control" placeholder="Discount" runat="server"></asp:TextBox>
                        </div>
                        <div class="form-group col-md-4">
                            <label>Tax</label>
                            <asp:TextBox ID="txtTax"  CssClass="form-control" placeholder="Tax" runat="server"></asp:TextBox>
                        </div>
                        <div class="form-group col-md-4">
                            <label>Total Amount</label>
                            <asp:TextBox ID="txtTotalAmount" EnableViewState="true"  CssClass="form-control" placeholder="Total Amount" runat="server"></asp:TextBox>
                        </div>
                    </div>`enter code here`
                <br />
                <div class="form-row">
                    <div class="form-group col-md-3">
                        <asp:Button ID="btnSave" runat="server" CssClass="btn btn-success btn-group-lg" Text="Save" OnClick="btnSave_Click" />
                        <a href="ProductList.aspx" id="btnCancle" runat="server" class="btn btn-danger">Cancel</a>
                    </div>
                </div>
        </form>
        </div>
    </div>
</asp:Content>

0 个答案:

没有答案