我的用户控件是这样的:
<asp:TextBox ID="_txtLeadDate" runat="server" ReadOnly="true">
</asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="_txtLeadDate" ErrorMessage="You cannot select a day earlier than today!" OnServerValidate="CustomValidator2_ServerValidate" SetFocusOnError="True">
</asp:CustomValidator>
服务器端代码是:
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args){
if (some condition){
args.IsValid = false;
}else{
args.IsValid = true;
}
}
但是页面显示错误:
'ASP.usercontrol_preordercontrol_ascx' does not contain a definition for 'CustomValidator2_ServerValidate' and no extension method 'CustomValidator2_ServerValidate' accepting a first argument of type 'ASP.usercontrol_preordercontrol_ascx' could be found (are you missing a using directive or an assembly reference?)
最佳答案
确保以下事件
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
在您的 UserControl(.ascx) 后面的代码中声明,而不是您调用此 UserControl 的 .aspx 表单。
关于c# - 自定义验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799007/