好的,我在这里和其他一些网站上关注了多个用于多个asp CheckBox的CustomValidator,并且在检查复选框时实际验证它有点困难。
虽然我尝试了各种各样的答案,但我特别关注了一个。 我已经知道它将显示在验证摘要中,但是当选中复选框时,它们仍会显示在摘要中它们不是。我不知道我做错了什么,或者我错过了什么,但我希望额外的一双眼睛可以帮助我找到我错过的东西。
CheckBox的:
<div class="section">
<span>2</span>Induction Checklist:
</div>
<div class="inner-wrap">
<label>Introduction of relevant staff <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbStaffIntro" /></label>
<label>Comments: <asp:TextBox runat="server" ID="txtStaffIntroComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Location of all Exits <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbExitLocations" /></label>
<label>Comments: <asp:TextBox runat="server" ID="txtExitLocationComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Evacuation Assembly Point <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbEvacuationPoint" /></label>
<label>Comments: <asp:TextBox runat="server" ID="txtEvacuationPointComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Location of Fire Extinguishers <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbFireExtLocations" /></label>
<label>Comments: <asp:TextBox runat="server" ID="txtFireExtLocationsComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Location of Fire Hoses <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbFireHoseLoc" /></label>
<label>Comment: <asp:TextBox runat="server" ID="txtFireHoseLocComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Location of First Aid Kits <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbFirstAidLoc" /></label>
<label>Comment: <asp:TextBox runat="server" ID="txtFirstAidLocComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Location of Male / Female Toilets <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbLooLoc" /></label>
<label>Comment: <asp:TextBox runat="server" ID="txtLooLocComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Location of Lunch Rooms <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbLuncLoc" /></label>
<label>Comment: <asp:TextBox runat="server" ID="txtLunchLocComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Explain "No-Smoking" Policy in all Offices and Buildings <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbNoSmoke" /></label>
<label>Comment: <asp:TextBox runat="server" ID="txtNoSmokeComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Explain and Indicate "No-Go" Zones in Warehouse <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbNoGo" /></label>
<label>Comment: <asp:TextBox runat="server" ID="txtNoGoComment" /></label>
</div> <!-- end inner-wrap -->
<div class="inner-wrap">
<label>Other <asp:CheckBox CssClass="HitCheckbox" ValidationGroup="checkList" runat="server" ID="cbOther" /></label>
<label>Comment: <asp:TextBox runat="server" ID="txtOtherComment" /></label>
</div> <!-- end inner-wrap -->
客户端验证:
<script>
function CheckBoxRequired_ClientValidate(sender, e)
{
e.IsValid = jQuery(".HitCheckBox input:checkbox").is(':checked');
}
</script>
服务器端验证:
public void Required_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (cbStaffIntro.Checked);
args.IsValid = (cbExitLocations.Checked);
args.IsValid = (cbEvacuationPoint.Checked);
args.IsValid = (cbFireExtLocations.Checked);
args.IsValid = (cbFireHoseLoc.Checked);
args.IsValid = (cbFirstAidLoc.Checked);
args.IsValid = (cbLooLoc.Checked);
args.IsValid = (cbLuncLoc.Checked);
args.IsValid = (cbNoSmoke.Checked);
args.IsValid = (cbNoGo.Checked);
}
自定义验证器:
<!-- Checklist Validation Section cbStaffIntro -->
<asp:CustomValidator runat="server" ID="CustomValidatorSi" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="Introduction to relevant Staff Not Completed!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorEl" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="Emergancy Exit Locations Not Completed!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorEp" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="Evacuation Point Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorFe" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="Fire Extinguisher Locations Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorFh" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="Fire Hose Locations Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorFa" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="First Aid Locations Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorToilet" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="Toilet Locations Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorLr" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="Lunchroom Location Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorNsp" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="No Smoking Policy Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
<asp:CustomValidator runat="server" ID="CustomValidatorNg" ValidateEmptyText="true" ValidationGroup="checkList" EnableClientScript="true" Display="None" ErrorMessage="'No-Go' Zones Not Covered!" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
我希望这个工作的方式是所有复选框,除了标记为“其他”的复选框之外,需要在验证表单之前选择。
如上所述,我有这样的观点,即如果未检查它们,它们将显示在验证摘要中。我遇到的问题是,为什么它们在检查时仍然显示为未在验证摘要中检查。
这里缺少什么?或者是我不完全可能的事情? 在我忘记我正在为此项目使用.NET Framework 4之前。
编辑:感谢Scotty努力帮助我解决这个问题。应用提供的修复后,我还必须删除Javascript验证,我必须让代码按需工作。
答案 0 :(得分:3)
您正在使用相同的服务器端方法Required_ServerValidate
来查看所有CustomValidators中的ErrorMessages,以验证所有CustomValidator
控件并且不构建您的ErrorMessage动态。
您需要更改逻辑以针对每种情况动态构建错误消息。
首先,删除除1之外的所有CustomValidator,然后按如下所示修改代码。
这是一种方法......
<强> ASPX 强>
<asp:CustomValidator runat="server" ID="cvCheckList" ValidationGroup="checkList" EnableClientScript="true" Display="None" OnServerValidate="Required_ServerValidate" ClientValidationFunction="CheckBoxRequired_ClientValidate" />
确保在ValidationSummary中使用DisplayMode="SingleParagraph"
,以便html正确呈现。
代码背后
public void Required_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = true;
var sb = new StringBuilder();
sb.Append("<ul>");
if (!cbStaffIntro.Checked)
AppendErrorMessage(args, sb, "Introduction to relevant Staff Not Completed!");
if (!cbExitLocations.Checked)
AppendErrorMessage(args, sb, "Emergency Exit Locations Not Completed!");
if (!cbEvacuationPoint.Checked)
AppendErrorMessage(args, sb, "Evacuation Point Not Covered!");
if (!cbFireExtLocations.Checked)
AppendErrorMessage(args, sb, "Fire Extinguisher Locations Not Covered!");
if (!cbFireHoseLoc.Checked)
AppendErrorMessage(args, sb, "Fire Hose Locations Not Covered!");
if (!cbFirstAidLoc.Checked)
AppendErrorMessage(args, sb, "First Aid Locations Not Covered!");
if (!cbLooLoc.Checked)
AppendErrorMessage(args, sb, "Toilet Locations Not Covered!");
if (!cbLuncLoc.Checked)
AppendErrorMessage(args, sb, "Lunchroom Location Not Covered!");
if (!cbNoSmoke.Checked)
AppendErrorMessage(args, sb, "No Smoking Policy Not Covered!");
if (!cbNoGo.Checked)
AppendErrorMessage(args, sb, "'No-Go' Zones Not Covered!");
sb.Append("</ul>");
cvCheckList.ErrorMessage = sb.ToString();
}
private static void AppendErrorMessage(ServerValidateEventArgs args, StringBuilder sb, string message)
{
args.IsValid = false;
sb.AppendFormat("<li>{0}</li>", message);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
Response.Redirect("ValidationSuccessful.aspx");
}