您好我无法确定自定义验证控件的问题所在。背景是我在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;
}