如何获取动态Gridview文本框模板列值总和 [英] How to get dynamic Gridview textbox template column value sum

查看:102
本文介绍了如何获取动态Gridview文本框模板列值总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我创建了一个动态网格视图,其中包含3个名为1)项目名称,2)数量,3)价格的文本框.现在我想获取标签中所有价格文本框的总计.

这是在gridview中动态添加新行的代码:-

Hello Friends,

i have create an dynamic grid view with 3 text box named 1) item name, 2)qty , 3)price. now i want to get Total of all the price text box in label.

here is code for dynamic add new row in gridview :-

private void AddNewRowToGrid()
    {
        try
        {
            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
                        TextBox box1 = (TextBox)GrdProduct.Rows[rowIndex].Cells[1].FindControl("txtitemname");
                        TextBox box2 = (TextBox)GrdProduct.Rows[rowIndex].Cells[2].FindControl("txtqty");
                        TextBox box3 = (TextBox)GrdProduct.Rows[rowIndex].Cells[3].FindControl("txtprice");

                        drCurrentRow = dtCurrentTable.NewRow();

                        drCurrentRow["RowNumber"] = i + 1;
                        drCurrentRow["Column1"] = box1.Text;
                        drCurrentRow["Column2"] = box2.Text;
                        drCurrentRow["Column3"] = box3.Text;

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

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

            //Set Previous Data on Postbacks
            SetPreviousData();
        }
        catch (Exception ex)
        {
            lblerror.Visible = true;
            lblerror.InnerText = ex.Message.ToString();
        }
    }



现在,当textbox_changed事件触发时,我希望所有价格合计在一个标签中.

感谢所有人... !!!!



now i want Total of all the Price in one Label when textbox_changed event fire.

Thanx to all...!!!!

推荐答案

function calculateamount() {
      var txtTotal = 0;
        var passed = false;
        total = 0.00;
        var id = 0;
        totalDTH = 0.00;
        totalMCF = 0;
        // Get the gridview
        var grid = document.getElementById("<%= GridView1.ClientID%>");
       
        // Get all the input controls (can be any DOM element you would like)
        var inputs = grid.getElementsByTagName("input");
        
        // Loop through all the DOM elements we grabbed
        for (var i = 0; i < inputs.length; i++) {
            // In this case we are looping through all the Dek Volume and then the Mcf volume boxes in the grid and not an individual one and totalling them
            if (inputs[i].name.indexOf("lbl_amt") > 1) {
                if (inputs[i].value != "") {
                    totalDTH = totalDTH + MathRound(parseFloat(inputs[i].value));
                }
            }
        }
        document.getElementById("<%= txtamttotal.ClientID %>").value = MathRound(totalDTH);
        
    }



if(inputs [i] .name.indexOf("lbl_amt")> 1)//您应在此处使用gridview中价格的文本框.

在keyup或onblur上调用calculateamount()函数


如果对您有帮助,请投票



if (inputs[i].name.indexOf("lbl_amt") > 1) // You should use here textbox of price which is in gridview.

call calculateamount() function on keyup or onblur


please vote if it helps you thanks


请参阅此主题
http://stackoverflow.com/questions/11951511/how-can-i-make-a-sum-for-rows-total-value [ http://stackoverflow.com/questions/6907306/gridview-textboxes-column-sum- using-jquery [ ^ ]
Refer this threads
http://stackoverflow.com/questions/11951511/how-can-i-make-a-sum-for-rows-total-value[^]
http://stackoverflow.com/questions/6907306/gridview-textboxes-column-sum-using-jquery[^]


这篇关于如何获取动态Gridview文本框模板列值总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆