我有一个Web应用程序,其中包含一个页面,其中包含一个表单(内置在详细信息视图中)以及一个用户控件。页面上的表单是一个基本表单,其中包含一些要输入的字段和一个复选框。我在document.ready上运行jQuery,隐藏了拥有用户控件的分区,如果选中了pages表单中的复选框,则会触发jQuery以显示用户控件。
两种表单都使用asp验证工具进行验证。
如果我填写表格,选中该框,并填写用户控件中的信息并提交,一切正常,但是如果我填写基本表格而不选中复选框以显示用户控件,我从用户控件获得验证错误(因为有一个提交和空值)。我怎么能绕过这个?
代码剪辑:
$("#submitMFields").hide();
$("#submitMFields").toggle(false);
$("[id$='chkM']").click(function () {
$("#submitMFields").toggle("slow");
});
<asp:TemplateField HeaderText="Currency">
<EditItemTemplate>
<asp:DropDownList ID="ddlCurrencyList"
runat="server"
DataTextField="strCultureName"
DataValueField="strCulture"
DataSource='<%# CodeLists.Currency() %>'
AppendDataBoundItems="true" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Info?">
<EditItemTemplate>
<asp:CheckBox runat="server" ID="chkM" />
<div id="submitMFields">
<cms:Control runat="server" ID="initialM" />
</div>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField InsertText="Submit" ShowCancelButton="False"
ShowInsertButton="True" />
在用户控件中,使用了这样的验证 -
<asp:RequiredFieldValidator
id="rfvStartDate"
runat="server"
ControlToValidate="txtStart"
ErrorMessage="Start Date is Required"
Display="dynamic">*
</asp:RequiredFieldValidator>
<ajax:ValidatorCalloutExtender
ID="VCEStart"
runat="server"
TargetControlID = "rfvStartDate" />
答案 0 :(得分:1)
您可以使用ClientValidationFunction
在客户端控制它:
注意:我的考试是使用jQuery,你可以使用任何你想要的DOM选择(document.getElemenyById..etc)
<asp:RequiredFieldValidator
id="rfvStartDate"
ClientValidationFunction="rfvStartDateClientValidate"
ControlToValidate="txtTitle"
runat="server"
ControlToValidate="txtStart"
ErrorMessage="Start Date is Required"
Display="dynamic">*
</asp:RequiredFieldValidator>
<ajax:ValidatorCalloutExtender
ID="VCEStart"
runat="server"
TargetControlID = "rfvStartDate" />
function rfvStartDateClientValidate(sender, args) {
//example below, put your client-side logic here
//toggling the fields will only hide the validators will still fire
//disable them based on the checkbox
var v = $('#<%=txtStart.ClientID%>');
if (v.val() == '') {
args.IsValid = false; // field is empty
v.addClass('requiredHighlight');
}
else {
v.removeClass('requiredHighlight');
}
}