我正在尝试为在Kentico v10.0.51和.NET Framework 4.6上运行的自定义表单控件实现一些自定义服务器端验证逻辑。我希望这个逻辑在提交事件上运行,我想在自定义表单控件的代码隐藏中定义逻辑。如何在提交按钮单击事件处理程序中使表单无效或停止处理?例如,参见附件以减少测试用例。
using System;
using CMS.FormEngine.Web.UI;
using CMS.Helpers;
// ReSharper disable ArrangeAccessorOwnerBody
namespace CMSApp.CMSFormControls.Custom
{
public partial class ServerSideValidator : FormEngineUserControl
{
public override object Value
{
get { return txtValue.Value; }
set { txtValue.Value = ValidationHelper.GetString(value, string.Empty); }
}
protected override void OnInit(EventArgs e)
{
Form.SubmitButton.Click += SubmitButtonOnClick;
base.OnInit(e);
}
private void SubmitButtonOnClick(object sender, EventArgs e)
{
var valid = CustomValidationHelper.ServerSideValidationMethod(Value);
if (!valid)
{
//TODO: Invalidate the form before save or notify. (Form.?)
}
}
}
}
使用覆盖的方法:
/// <summary>
/// Returns true if a color is selected. Otherwise, it returns false and displays an error message.
/// </summary>
public override bool IsValid()
{
if ((string)Value != "")
{
return true;
}
else
{
// Sets the form control validation error message
this.ValidationError = "Please choose a color.";
return false;
}
}
在else
语句中,对要验证的字段或表达式执行验证,并根据您要验证的内容返回消息。