你可以试试CustomValidator
这个,如下图
<head runat="server">
<title></title>
<script>
function validate(sender, arg) {
debugger;
var h1_total =
(document.getElementById("txtKS_1").value == "" ? 0 : parseFloat(document.getElementById("txtKS_1").value)) +
(document.getElementById("txtKS_2").value == "" ? 0 : parseFloat(document.getElementById("txtKS_2").value)) +
(document.getElementById("txtKS_3").value == "" ? 0 : parseFloat(document.getElementById("txtKS_3").value)) +
(document.getElementById("txtKS_4").value == "" ? 0 : parseFloat(document.getElementById("txtKS_4").value));
var H2A_total =
(document.getElementById("txtH2A_1").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_1").value)) +
(document.getElementById("txtH2A_2").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_2").value)) +
(document.getElementById("txtH2A_3").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_3").value)) +
(document.getElementById("txtH2A_4").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_4").value)) +
(document.getElementById("txtH2A_5").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_5").value)) +
(document.getElementById("txtH2A_6").value == "" ? 0 : parseFloat(document.getElementById("txtH2A_6").value));
if (h1_total != H2A_total)
arg.IsValid = false;
else
arg.IsValid = true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="txtKS_1" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtKS_2" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtKS_3" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtKS_4" ClientIDMode="Static" />
<br />
<asp:TextBox runat="server" ID="txtH2A_1" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_2" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_3" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_4" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_5" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtH2A_6" ClientIDMode="Static" />
<br />
<asp:CustomValidator runat="server" ID="vali1" ErrorMessage="Values must be same." ClientValidationFunction="validate" ValidationGroup="validate" />
<asp:Button runat="server" ID="btn" Text="Click me" ValidationGroup="validate" />
</div>
</form>
</body>
需要注意的事项:
我ClientIDMode="Static"
只是为了使代码紧凑而使用它,如果可以的话请忽略它并使用document.getElementById('<%= txtH2A_6.ClientID %>')
等。