asp.net - 自定义验证未在 formview 控件中触发
问题描述
您好,我无法确定自定义验证控件的问题所在。背景是我在formview的EditTemplate中有一个formview控件,我有一个日期字段。我想验证日期是否过去。如果是则导致错误消息并且基本上不更新。谢谢指导。
日期字段为 TxtProjectPlannedEndDate
<td>
<asp:TextBox ID="TxtProjectPlannedEndDate" runat="server" class="form-
control" Text='<%# Eval("Planned Completion Date", "{0:dd-MMM-yyyy}")%>'
/>
<asp:CustomValidator ID="CustomValidatorPlannedEndDate" runat="server"
ErrorMessage="Planned End date can't be in the past"
ClientValidationFunction="ValidatePastDates" CssClass="text-danger"
ControlToValidate="TxtProjectPlannedEndDate" Display="Dynamic">
</asp:CustomValidator>
</td>
FormView 的 EditTemplate 中的按钮
<asp:LinkButton ID="UpdateButton" CommandName="Update" runat="server"
ToolTip="Save Modifications" CssClass="btn btn-primary" Text="Save"
CausesValidation="true" />
<asp:LinkButton
ID="CancelButton" CommandName="Cancel" runat="server" ToolTip="Cancel"
CssClass="btn btn-warning" Text="Abort" CausesValidation="false" />
我的脚本
function ValidatePastDates(source, args) {
debugger;
var selectedDate = parseDate(document.getElementById('MainContent_FrmVwProjectDetails_TxtProjectPlannedEndDate').value);
var projectStatus = document.getElementById('MainContent_FrmVwProjectDetails_ddlStatus');
var projectStatusValue = projectStatus.options[projectStatus.selectedIndex].value;
var userRole = document.getElementById('MainContent_hdnUserRole').value;
var now = new Date();
var dt1 = Date.parse(now);
dt2 = Date.parse(selectedDate);
debugger;
if (userRole.indexOf("Basic User") >= 0 || userRole.indexOf("Project Manager") >= 0)
// if user is basic user or project manager only the validate for past dates
// Administrator or manager roles can have dates in the past for the purpose of corrections that might need to happen
{
if (dt2 < dt1) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
}
function parseDate(s) {
var months = {
jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5,
jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11
};
var p = s.split('-');
return new Date(p[2], months[p[1].toLowerCase()], p[0]);
debugger;
}
解决方案
推荐阅读
- html - col-sm-12 在智能手机上没有 100% 的宽度 - 我做错了什么?
- c - 如何在 Fedora 中编译 -static ncursesw
- reactjs - 将 React 包含为 webpack 外部结果会导致找不到模块“react”
- javascript - 车轮事件 PreventDefault 不取消车轮事件
- python - Visual Studio Code - Code Runner:'python' 不是内部或外部命令、可运行程序或批处理文件
- apache-spark - 明确火花数据框中的分区数
- javascript - 如何使用javascript将占位符颜色更改为透明
- r - 如何使用 Tidyverse 对几列进行分组并计算百分比并创建新的 df?
- python - 尝试运行 pyinstaller 创建的 .exe 文件时“执行脚本失败”
- css - 完整的单词应该在同一行呈现