我有一个简单的方法:如果选中'是'单选按钮,则会使文本框成为必需的情况。我提供了客户端和服务器端验证。我发现的是:
为什么客户端在验证方面正常工作,但没有呈现错误消息并继续服务器?
asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
function testClientValidation(src, args) {
if ($('#<%=rblstTest.ClientID%>' + ' input:checked').length == 1) {
if ($('#<%=rblstTest.ClientID%>' + ' input:checked').val().toLowerCase() == "yes") {
args.isValid = !($('#<%=txtTest.ClientID%>').val() == "");
} else {
args.isValid = true;
}
} else {
args.isValid = true;
}
alert(args.isValid);
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<asp:RadioButtonList ID="rblstTest" RepeatDirection="Horizontal" RepeatLayout="Flow" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtTest" runat="server"/>
<asp:CustomValidator ID="cust" ControlToValidate="rblstTest"
OnServerValidate="testSeverValidation"
ClientValidationFunction="testClientValidation"
Display="Dynamic"
ErrorMessage="Error!" runat="server"/>
<asp:LinkButton runat="server" ID="lnkSubmit" Text="Submit" />
</div>
</asp:Content>
答案 0 :(得分:4)
使用args.IsValid
代替args.isValid
function testClientValidation(src, args) {
if ($('#<%=rblstTest.ClientID%>' + ' input:checked').length == 1) {
if ($('#<%=rblstTest.ClientID%>' + ' input:checked').val().toLowerCase() == "yes") {
args.IsValid = !($('#<%=txtTest.ClientID%>').val() == "");
} else {
args.IsValid = true;
}
} else {
args.IsValid = true;
}
alert(args.IsValid);
}
答案 1 :(得分:0)
我添加了Hidden Field
新添加的内容。
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.js"></script>
<script language="javascript" type="text/javascript">
function testClientValidation(src, args) {
debugger;
if (document.getElementById('<%= hdn.ClientID %>').value == '1') {
var txtBox = document.getElementById('<%= txtTest.ClientID%>');
if (txtBox.value == '') {
document.getElementById('cust').style.display = 'block';
document.getElementById('cust').innerHTML = 'Error!'
document.getElementById('<%= hdn.ClientID %>').value = "1";
args.IsValid = false;
return false;
}
else {
document.getElementById('cust').style.display = 'none';
document.getElementById('cust').innerHTML = '';
document.getElementById('<%= hdn.ClientID %>').value = "0";
args.IsValid = true;
}
}
else {
document.getElementById('cust').style.display = 'none';
document.getElementById('cust').innerHTML = '';
document.getElementById('<%= hdn.ClientID %>').value = "0";
args.IsValid = true;
}
return true;
}
$(document).ready(function () {
$("span input[type='radio']").click(function () {
debugger;
if ($(this).val() == 'Yes') {
document.getElementById('<%= hdn.ClientID %>').value = "1";
if (document.getElementById('<%= txtTest.ClientID%>').value == '') {
Page_IsValid = false;
document.getElementById('<%= hdn.ClientID %>').value = "1";
}
else {
Page_IsValid = true;
}
}
else {
document.getElementById('<%= hdn.ClientID %>').value = "0";
Page_IsValid = true;
}
return true;
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div>
<asp:RadioButtonList ID="rblstTest" RepeatDirection="Horizontal" RepeatLayout="Flow"
runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtTest" runat="server" />
<asp:CustomValidator ClientIDMode="Static" ID="cust" OnServerValidate="testSeverValidation"
ClientValidationFunction="testClientValidation" Display="Dynamic" ErrorMessage="Error!"
runat="server" />
<asp:LinkButton runat="server" ID="lnkSubmit" Text="Submit" />
<asp:HiddenField ID="hdn" runat="server" Value="0" />
</div>
</asp:Content>
有关详细信息,请参阅参考 here