【问题标题】:How to bubble up validation error from child control to parent control如何将验证错误从子控件冒泡到父控件
【发布时间】:2014-07-08 19:07:03
【问题描述】:

我的 asp.net 网页表单页面有一个用户控件 (Control1.ascx) 和一个保存按钮。 Control1 包含另一个用户控件 Control2.ascx,其中包含多个自定义验证器。

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
 if (somecheck1)
     args.IsValid = false;
 else
     args.IsValid = true;
}

protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
{
 if (somecheck2)
     args.IsValid = false;
 else
     args.IsValid = true;
}

如果 Control2 上的任何验证器失败,我不希望 Control1.Save 代码执行。如何实现?我正在考虑在 Control2 上添加一个 Valid 属性,并在“保存”按钮代码中检查该属性。这样的做法合理吗?另外应该在 Valid 属性中做什么?

编辑:感谢您的回复,但我无法解决这个问题。我在Handling of validation errors from child controls

发布了一个带有更具体代码示例的不同问题

【问题讨论】:

    标签: c# asp.net webforms custom-controls custom-validators


    【解决方案1】:

    执行page.validate("customValidator1");,其中 customValidator1 是您的验证组。然后触发Page.IsValid();如果第一个组验证失败,那么 Page.IsValid() 将返回 false。在你的逻辑中实现它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      相关资源
      最近更新 更多