locked
Total Value is not displaying in preview page RRS feed

  • Question

  • User1216627406 posted

    Very sorry to bother you guys again experts but I have been playing with this code now for entire day literally and I could not figure out the issue.

    Again, this is Gridview which allows users to dynamically add new rows.

    In our case, there is a control called txtatxpayerret. This takes numeric values.

    Users can enter 25 in the first row, add another row and enter another 60 for a total of 85.

    This 85 is displayed in a lable with ID of lblTotal.

    Then once the user is done with entering his/her values, s/he clicks the NEXT button to go to the preview page.

    This is where I am having issues.

    On Preview page, user can see the two values entered into two rows. Values of 25 and 85.


    The issue is that the total of those two values are not getting displayed in another label with control ID of lblPrevTotal.

    When press f12 to try to see what is going on, I can see total amount of 85 displayed on lblPrevTotal but it is just not displaying.

    What am I overlooking?

    If you run the code I am pasting below, it works except show total value in preview screen.

    Thanks again for the help.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="grivTest.aspx.cs" Inherits="grivTest" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="scriptManager" runat="server">
        </asp:ScriptManager>
        <asp:MultiView ID="myMultiView" runat="server">
            <asp:View ID="vwPersonalInfo" runat="server">
                <table border="1" style="width: 100%; text-align: center; margin-left: -30px;">
                    <tr>
                        <td style="width: 100%; vertical-align: text-top;">
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:GridView ID="Gridview1" Style="table-layout: fixed;" runat="server" ShowFooter="true"
                                        AutoGenerateColumns="false" OnRowDataBound="Gridview1_RowDataBound" OnRowDeleting="Gridview1_RowDeleting">
                                        <Columns>
                                            <asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
                                            <asp:TemplateField HeaderText="Aircraft Registration #:">
                                                <HeaderStyle HorizontalAlign="Left" />
                                                <ItemTemplate>
                                                    <asp:TextBox ID="txtboatregNum" runat="server" Style="width: 83px;" class="form-control"></asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="TaxPayer Returned Value <br />As of Jan This Year">
                                                <ItemStyle HorizontalAlign="Left"></ItemStyle>
                                                <ItemTemplate>
                                                    <asp:TextBox ID="txttaxpayerret" runat="server" Style="width: 88px;" class="form-control txttaxpayerret"></asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="For Tax Office use Only <br /> (Tax Assessirs Value)">
                                                <ItemStyle HorizontalAlign="Left"></ItemStyle>
                                                <ItemTemplate>
                                                    <asp:TextBox ID="txtfortaxofficeonly" runat="server" Style="width: 80px; background-color: #ECECEC;"
                                                        ReadOnly="true" class="form-control"></asp:TextBox>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="">
                                                <HeaderStyle Width="70" />
                                                <ItemStyle Width="70" />
                                                <ItemTemplate>
                                                    <asp:Button ID="BtnAdd" runat="server" Text="Add" OnClick="BtnAdd_Click" CssClass="grvDelButton  btnclass" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:CommandField ShowDeleteButton="True">
                                                <ControlStyle CssClass="grvDelButton" />
                                            </asp:CommandField>
                                        </Columns>
                                    </asp:GridView>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </td>
                    </tr>
                </table>
                <table border="1" style="width: 100%; text-align: center">
                    <tr>
                        <td style="width: 24.2%;">
                            TOTAL&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;>
                        </td>
                        <td style="width: 23%;">
                            <asp:Label ID="lblTotal" Text="0" runat="server"></asp:Label>
                            <asp:HiddenField ID="hfvalue" runat="server" />
                        </td>
                        <td style="width: 62.8%;">
                        </td>
                    </tr>
                </table>
            </asp:View>
            <asp:View ID="vwPreview" runat="server">
                <table border="1" style="width: 100%; text-align: center;">
                    <tr>
                        <td style="width: 100%; vertical-align: text-top;">
                            <table cellspacing="0" rules="all" border="1" width="100%" style="border-collapse: collapse;
                                table-layout: fixed;">
                                <tr>
                                    <th align="left" scope="col">
                                        Aircraft Registration #:
                                    </th>
                                    <th scope="col">
                                        TaxPayer Returned Value
                                        <br />
                                        As of Jan This Year
                                    </th>
                                    <th scope="col">
                                        For Tax Office use Only
                                        <br />
                                        (Tax Assessirs Value)
                                    </th>
                                    <th scope="col" style="width: 70px;">
                                        &nbsp;
                                    </th>
                                    <th scope="col">
                                        &nbsp;
                                    </th>
                                </tr>
                                <%foreach (System.Data.DataRow row in dtAirInfoTable.Rows)
                                    {%>
                                <tr>
                                    <td>
                                        <span class="form-control" style="width: 493px; background-color: cornsilk; color: #0093B2;
                                            font-weight: bold;">
                                            <%=row.ItemArray[1].ToString()%>
                                        </span>
                                    </td>
                                    <td align="left">
                                        <span class="form-control txttaxpayerret" style="width: 326px; background-color: cornsilk;
                                            color: #0093B2; font-weight: bold;">
                                            <%=row.ItemArray[2].ToString()%>
                                        </span>
                                    </td>
                                    <td align="left">
                                        <span class="form-control" style="width: 305px; color: #0093B2; font-weight: bold;
                                            background-color: #ECECEC;">
                                            <%=row.ItemArray[3].ToString()%>
                                        </span>
                                    </td>
                                </tr>
                                <% } %>
                            </table>
                        </td>
                    </tr>
                </table>
                <table border="1" style="width: 100%; text-align: center">
                    <tr>
                        <td width="24.2%">
                            TOTAL&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;>
                        </td>
                        <td style="width: 23%">
                            <asp:Label ID="lblTotalPrev" runat="server"></asp:Label>
                        </td>
                        <td style="width: 62.8%">
                        </td>
                    </tr>
                </table>
                <br />
            </asp:View>
        </asp:MultiView>
    </div>
    <br />
    <br />
    <div>
        <asp:Button ID="btnBack" Text="Back" class="btnclass" OnClick="onBack" runat="server" />
        <asp:Button ID="btnNext" Text="Next" class="btnclass" OnClick="onNext" runat="server" />
        <asp:Button ID="btnSave" Text="Save" class="btnclass" runat="server" />
    </div>
    <div>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
            function pageLoad() {
                totalcalculate();
                $('[id*=txttaxpayerret]').keyup(function () {
                    totalcalculate();
                });
            }
     
            function totalcalculate() {
                var total = 0;
                for (var i = 0; i < $('.txttaxpayerret').length; i++) {
                    var temp = $('.txttaxpayerret')[i].value;
                    temp != "" ? temp : temp = 0;
                    total = parseFloat(temp) + parseFloat(total);
                    $('[id*=lblTotal]').html(total);
                    $('[id*=hfvalue]').val(total);
                }
            }      
        </script>
        <style type="text/css">
            body
            {
                font-family: Arial;
                font-size: 10pt;
            }
            table
            {
                border: 1px solid #ccc;
            }
            table th
            {
                background-color: #F7F7F7;
                color: #333;
                font-weight: bold;
            }
            table th, table td
            {
                padding: 5px;
                border-color: #ccc;
            }
            .btnclass
            {
                color: #FFF;
                background-color: #204D74;
                border-color: #122B40;
                display: inline-block;
                padding: 6px 12px;
                margin-bottom: 0px;
                font-size: 14px;
                font-weight: 400;
                line-height: 1.42857;
                text-align: center;
                white-space: nowrap;
                vertical-align: middle;
                cursor: pointer;
                -moz-user-select: none;
                background-image: none;
                border: 1px solid transparent;
                border-radius: 4px;
            }
        </style>
    </div>
    </form>
    </body>
    </html> 
    
    C#
    
    
    
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI;
    using System.Data.SqlClient;
    using System.Net.Mail;
    using System.Net;
    using System.Threading;
    using System.Net.Mime;
    using System.Drawing;
    using System.Collections.Specialized;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetInitialRow();
        }
    }
     
    private void SetInitialRow()
    {
        myMultiView.ActiveViewIndex = 0;
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dt.Rows.Add(dr);
        ViewState["CurrentTable"] = dt;
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }
     
    private void AddNewRowToGrid()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtboatregNum");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txttaxpayerret");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtfortaxofficeonly");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                    rowIndex++;
                }
     
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;
                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
     
        else
        {
            Response.Write("ViewState is null");
        }
     
        SetPreviousData();
     
    }
     
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtboatregNum");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txttaxpayerret");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtfortaxofficeonly");
                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();
                    rowIndex++;
                }
            }
        }
    }
     
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
     
    protected override void OnPreRender(EventArgs e)
    {
        if (myMultiView.ActiveViewIndex == myMultiView.Views.Count - 1)
        {
            FillSummary();
        }
     
        btnBack.Visible = myMultiView.ActiveViewIndex > 0;
        btnNext.Visible = myMultiView.ActiveViewIndex < myMultiView.Views.Count - 1;
        btnSave.Visible = myMultiView.ActiveViewIndex == myMultiView.Views.Count - 1;
        base.OnPreRender(e);
    }
     
    public DataTable dtAirInfoTable;
    private void FillSummary()
    {
        // lblcitylist.Text = citylist.Text;
        lblTotalPrev.Text = hfvalue.Value;
        if (ViewState["TempData"] != null)
        {
            dtAirInfoTable = (DataTable)ViewState["TempData"];
        }
    }
     
    protected void onNext(object sender, EventArgs e)
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
     
                for (int i = 1; i <= Gridview1.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtboatregNum");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txttaxpayerret");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtfortaxofficeonly");
                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                    rowIndex++;
                }
     
                ViewState["TempData"] = dtCurrentTable;
            }
        }
     
        myMultiView.ActiveViewIndex = 1;
    }
     
    protected void onBack(object sender, EventArgs e)
    {
        lblTotal.Text = lblTotalPrev.Text;
        myMultiView.ActiveViewIndex = 0;
    }
     
    protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = 0;
        int rowID = e.RowIndex;
        lblTotal.Text = "0";
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= Gridview1.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtboatregNum");
                    TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txttaxpayerret");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtfortaxofficeonly");
                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                    rowIndex++;
                }
     
                if (e.RowIndex < dtCurrentTable.Rows.Count)
                {
                    dtCurrentTable.Rows.Remove(dtCurrentTable.Rows[rowID]);
                }
            }
     
            ViewState["CurrentTable"] = dtCurrentTable;
            Gridview1.DataSource = dtCurrentTable;
            Gridview1.DataBind();
        }
     
        SetPreviousData();
    }
     
    

    Friday, July 28, 2017 7:22 PM

Answers

  • User2103319870 posted

    When press f12 to try to see what is going on, I can see total amount of 85 displayed on lblPrevTotal but it is just not displaying.

    Change your javascript function like below

     function totalcalculate() {
                        var total = 0;
                        for (var i = 0; i < $('.txttaxpayerret').length; i++) {
                            var temp = $('.txttaxpayerret')[i].value;
                            temp != "" ? temp : temp = 0;
                            total = parseFloat(temp) + parseFloat(total);
                            $('#lblTotal').html(total);
                            $('[id*=hfvalue]').val(total);
                        }
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 28, 2017 8:34 PM

All replies

  • User2103319870 posted

    When press f12 to try to see what is going on, I can see total amount of 85 displayed on lblPrevTotal but it is just not displaying.

    Change your javascript function like below

     function totalcalculate() {
                        var total = 0;
                        for (var i = 0; i < $('.txttaxpayerret').length; i++) {
                            var temp = $('.txttaxpayerret')[i].value;
                            temp != "" ? temp : temp = 0;
                            total = parseFloat(temp) + parseFloat(total);
                            $('#lblTotal').html(total);
                            $('[id*=hfvalue]').val(total);
                        }
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 28, 2017 8:34 PM
  • User1216627406 posted

    WOW, that is incredible!

    Worked like a dream.

    Thanks very much for all the help A2H.

    I really appreciate this.

    Friday, July 28, 2017 8:58 PM
  • User1216627406 posted

    I am so sorry. I was so excited that I forgot to mark your solution as the correction solution

    Thanks again.

    Saturday, July 29, 2017 2:33 AM