当点击btnSave时,它将调用function check()。
如果check()返回true,页面将回发并调用btnSave_Click事件。
但我认为应该在调用fucntion check()之前先检查RequiredFieldValidator。
我该如何解决?
在Default.aspx中
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestValidation._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<script type="text/javascript">
function check() {
return confirm("Real?");
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Display="Dynamic" ValidationGroup="vldGrp">AA</asp:RequiredFieldValidator>
<asp:Button ID="btnSave" runat="server" ValidationGroup="vldGrp" Text="Save" OnClientClick="return check();" />
<asp:ValidationSummary ID="vldSum" runat="server" DisplayMode="List" ShowSummary="False" ValidationGroup="vldGrp" ShowMessageBox="True" />
</div>
</form>
</body>
</html>
在Default.aspx.vb
中
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
End Sub
End Class
答案 0 :(得分:1)
首先检查函数内部所需的字段验证器,然后执行函数内的操作:
function check() {
var validation = <%= RequiredFieldValidator1.ClientID %>;
if(validation.isValid()){
return confirm("Real?");
}
}
如果有许多必需的字段验证器,并且您需要调用所有这些,请尝试:
function check() {
if (Page_ClientValidate()) {
return confirm("Real?");
}
}
甚至基于ValidationGroup的名称(如果您只需要调用选定的验证器)
function check() {
if (Page_ClientValidate('your group name')) {
return confirm("Real?");
}
}
否则你也可以检查服务器端的验证,
Sub ValidateBtn_Click(sender As Object, e As EventArgs)
Page.Validate()
If (Page.IsValid) Then
DoSomething()
End If
End Sub