简体   繁体   English

使用Javascript在Gridview中求和

[英]Sum Calculation in Gridview Using Javascript

I have a problem here..Before I proceed I will try to explain my problem , I have a grid view with a check boxes, text box(Marks1), text box(Marks2) and a label to show the sum of Marks1 and Marks 2. 我在这里有一个问题。在继续之前,我将尝试解释我的问题,我有一个带有复选框,文本框(Marks1),文本框(Marks2)和标签的网格视图,以显示Marks1和Marks之和2。

If I update the particular marks1 and marks 2 whose check box is checked, immediately sum should be changed or updated. 如果我更新了选中其复选框的特定mark1和mark 2,则应立即更改或更新总和。

Here is Java Script page: 这是Java脚本页面:

<script type="text/javascript">
        function SelectChange(ChkId, txt1, txt2, total) {
            var chk = document.getElementById(ChkId);
            UpdateField(ChkId, txt1, txt2,total)

        }

        function UpdateField(ChkId, txt1, txt2, total) {
            if (document.getElementById(chkID).checked == true) {
                var txtval1 = document.getElementById(txt1).value != "" ? document.getElementById(txt1).value : "0";
                var txtval2 = document.getElementById(txt2).value != "" ? document.getElementById(txt2).value : "0";
                var total1 = parseInt(txtval1) + parseInt(txtval2);
                document.getElementById(total).innerHTML = total1;
                if (document.getElementById(txt1).value == "" && document.getElementById(txt2).value == "") {
                    document.getElementById(total).innerHTML = "";
                    document.getElementById(txt1).disabled = false;
                    document.getElementById(txt2).disabled = false;
                }


            } else {
                document.getElementById(txt1).disabled = true;
                document.getElementById(txt2).disabled = true;

            }
        }

    </script>

and I used Row Data bound event for setting attributes of check box, and text box.. like this: 并且我使用行数据绑定事件来设置复选框和文本框的属性。如下所示:

protected void Gdview_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txt1 =(TextBox) e.Row.FindControl("Txtmark1");
                TextBox txt2 = (TextBox)e.Row.FindControl("Txtmark2");
                Label lb1 = (Label)e.Row.FindControl("lbTotal");
                CheckBox chk = (CheckBox)e.Row.FindControl("Checker");

                chk.Attributes["onclick"] = "javascript:return SelectChange('" + chk.ClientID + "','" + txt1.ClientID + "','" + txt2.ClientID + "','" + lb1.ClientID + "')";
                txt1.Attributes["onKeyup"] = "javascript:return UpdateField('" + chk.ClientID + "','" + txt1.ClientID + "','" + txt2.ClientID + "','" + lb1.ClientID + "')";
                txt2.Attributes["onKeyup"] = "javascript:return UpdateField('" + chk.ClientID + "','" + txt1.ClientID + "','" + txt2.ClientID + "','" + lb1.ClientID + "')";


            }
        }

It is not working.. I cant understand why?? 它不起作用..我不明白为什么? Isn't it the correct way to add attributes to the check boxes,text boxes??? 将属性添加到复选框,文本框不是正确的方法吗??? Or Should I have to change the post back of Label of Sum?? 还是我应该更改Sum标签的回发?

Watch the ChkId parameter of the UpdateField function. 观察UpdateField函数的ChkId参数。 You are using it as chkID. 您将其用作chkID。 It should be exactly the same, case sensitive wise. 它应该完全相同,区分大小写。

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

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