如何在提交按钮单击事件处理程序中使表单无效或停止处理?

问题描述 投票:0回答:1

我正在尝试为在Kentico v10.0.51和.NET Framework 4.6上运行的自定义表单控件实现一些自定义服务器端验证逻辑。我希望这个逻辑在提交事件上运行,我想在自定义表单控件的代码隐藏中定义逻辑。如何在提交按钮单击事件处理程序中使表单无效或停止处理?例如,参见附件以减少测试用例。

https://pastebin.com/Lnt0Rn9y

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.?)
            }
        }
    }
}
c# asp.net forms validation kentico
1个回答
2
投票

使用覆盖的方法:

/// <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语句中,对要验证的字段或表达式执行验证,并根据您要验证的内容返回消息。

© www.soinside.com 2019 - 2024. All rights reserved.