简体   繁体   English

在GridView中回发后,JavaScript代码将无法运行

[英]Javascript Code won't run after postback in gridview

The below code generate a row in GridView when clicking a Button that is residing inside a GridView Footer. 当单击位于GridView页脚内的Button时,以下代码在GridView中生成一行。

i used the JavaScript to make some calculation on the grid-view to get total amount and net total. 我使用JavaScript在网格视图上进行了一些计算,以获取总金额和总净额。

But the issue is when i add new row to the grid the calculated values disappear from the grid view. 但是问题是,当我在网格中添加新行时,计算值从网格视图中消失。

Before adding the row. 在添加行之前。

在此处输入图片说明

After adding the row. 添加行之后。 在此处输入图片说明

JavaScript Code JavaScript代码

    $(document).ready(function () {
       function multInputs() {
           var $mult = 0;
           var $multNet = 0;
           var $multGrand = 0;
           $("tr.txtMult").each(function () {
               var $UnitPrice = $('.UnitPrice', this).val();
               var $Quantity = $('.Quantity', this).val();
               var $Discount = $('.Discount', this).val();
               var $total = (($UnitPrice) * ($Quantity));
               var $Nettotal = (($UnitPrice) * ($Quantity) - ($Discount) * ($Quantity));

               $mult += $total;
               $multNet += $Nettotal;
           });

           $("tr.txtMult").each(function () {
               var $UnitPrice = $('.UnitPrice', this).val();
               var $Quantity = $('.Quantity', this).val();
               var $Discount = $('.Discount', this).val();
               var $total = (($UnitPrice) * ($Quantity));
               var $Nettotal = (($UnitPrice) * ($Quantity) - ($Discount) * ($Quantity));

               $('.multTotal', this).text(parseFloat($total).toFixed(2));
               $('.multNet', this).text(parseFloat($Nettotal).toFixed(2));

           });
           $(".lblGrandAmount").text(parseFloat($mult).toFixed(2));
           $(".lblNetTotal").text(parseFloat($multNet).toFixed(2));
       }                          

    $(".txtMult input").on('keyup mouseup', multInputs);
    function multInputs() {
        var $mult = 0;
        var $multNet = 0;
        var $multGrand = 0;
        $("tr.txtMult").each(function () {
            var $UnitPrice = $('.UnitPrice', this).val();
            var $Quantity = $('.Quantity', this).val();
            var $Discount = $('.Discount', this).val();
            var $total = (($UnitPrice) * ($Quantity));
            var $Nettotal = (($UnitPrice) * ($Quantity) - ($Discount) * ($Quantity));

            $mult += $total;
            $multNet += $Nettotal;   

        });

        $("tr.txtMult").each(function () {
            var $UnitPrice = $('.UnitPrice', this).val();
            var $Quantity = $('.Quantity', this).val();
            var $Discount = $('.Discount', this).val();
            var $total = (($UnitPrice) * ($Quantity));
            var $Nettotal = (($UnitPrice) * ($Quantity) - ($Discount) * ($Quantity));

            $('.multTotal', this).text(parseFloat($total).toFixed(2));
            $('.multNet', this).text(parseFloat($Nettotal).toFixed(2));

        });
        $(".lblGrandAmount").text(parseFloat($mult).toFixed(2));
        $(".lblNetTotal").text(parseFloat($multNet).toFixed(2));

    }
});

C# code C#代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }

 protected void griditem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.CssClass = "txtMult";
        }    
    }


    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }

    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++)
                {
                    //extract the TextBox values
                    DropDownList dllitem = (DropDownList)griditem.Rows[rowIndex].Cells[1].FindControl("dllitem");
                    TextBox txtUnitprice = (TextBox)griditem.Rows[rowIndex].Cells[2].FindControl("txtUnitprice");
                    TextBox txtDiscount = (TextBox)griditem.Rows[rowIndex].Cells[3].FindControl("txtDiscount");
                    TextBox txtQuantity = (TextBox)griditem.Rows[rowIndex].Cells[4].FindControl("txtQuantity");
                    Label lblTotal = (Label)griditem.Rows[rowIndex].Cells[5].FindControl("lblTotal");
                    Label lblnet = (Label)griditem.Rows[rowIndex].Cells[6].FindControl("lblnet");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["Sr.No"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Column1"] = dllitem.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["Column2"] = txtUnitprice.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = txtDiscount.Text;
                    dtCurrentTable.Rows[i - 1]["Column4"] = txtQuantity.Text;
                    dtCurrentTable.Rows[i - 1]["Column5"] = lblTotal.Text;
                    dtCurrentTable.Rows[i - 1]["Column6"] = lblnet.Text;


                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                griditem.DataSource = dtCurrentTable;
                griditem.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        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++)
                {
                    DropDownList dllitem = (DropDownList)griditem.Rows[rowIndex].Cells[1].FindControl("dllitem");
                    TextBox txtUnitprice = (TextBox)griditem.Rows[rowIndex].Cells[2].FindControl("txtUnitprice");
                    TextBox txtDiscount = (TextBox)griditem.Rows[rowIndex].Cells[3].FindControl("txtDiscount");
                    TextBox txtQuantity = (TextBox)griditem.Rows[rowIndex].Cells[4].FindControl("txtQuantity");
                    Label lblTotal = (Label)griditem.Rows[rowIndex].Cells[5].FindControl("lblTotal");
                    Label lblnet = (Label)griditem.Rows[rowIndex].Cells[6].FindControl("lblnet");

                    dllitem.SelectedValue = dt.Rows[i]["Column1"].ToString();
                    txtUnitprice.Text = dt.Rows[i]["Column2"].ToString();
                    txtDiscount.Text = dt.Rows[i]["Column3"].ToString();
                    txtQuantity.Text = dt.Rows[i]["Column4"].ToString();
                    lblTotal.Text = dt.Rows[i]["Column5"].ToString();
                    lblnet.Text = dt.Rows[i]["Column6"].ToString();

                    rowIndex++;
                }
            }
        }
    }    

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("Sr.No", 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)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dt.Columns.Add(new DataColumn("Column5", typeof(string)));
        dt.Columns.Add(new DataColumn("Column6", typeof(string)));

        dr = dt.NewRow();
        dr["Sr.No"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dr["Column5"] = string.Empty;
        dr["Column6"] = string.Empty;

        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        griditem.DataSource = dt;
        griditem.DataBind();
    }

You have to manually execute the JavaScript code that binds listeners to element within an UpdatePanel after a PostBack. 您必须手动执行在PostBack之后将侦听器绑定到UpdatePanel中的元素的JavaScript代码。 So create a javascript function that you can call again when needed, not just in the document ready. 因此,创建一个javascript函数,您可以在需要时再次调用它,而不仅仅是在准备好的文档中。

<script type="text/javascript">
    $(document).ready(function () {
        doDefaultStuff();
    });

    function doDefaultStuff() {
        //all the javascript that you would normally put in document ready
    }        
</script>

Then in code behind call that javascript function in the PostBack method. 然后在后面的代码中,在PostBack方法中调用该javascript函数。

protected void Button1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "doDefaultStuff", "doDefaultStuff();", true);
}

You need to trigger calculations again after partial post back. 部分回发后,您需要再次触发计算。 This can be done by first wrapping all your calculation script into a function. 这可以通过首先将所有计算脚本包装到一个函数中来完成。 Then call this function on document load and partial post back. 然后在文档加载和部分回发时调用此函数。

$(document).ready(function() {
    // trigger you calculations
    doMyStuff();
});

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(function() {
    // trigger you calculations
    doMyStuff();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM