从ASP继承:的ValidationSummary [英] inherit from asp:validationsummary

查看:146
本文介绍了从ASP继承:的ValidationSummary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在页面(或控制)的顶部有一个很好的格式化的错误消息的要求。

i have the requirement to show a nicely formatted error-message on top of the page (or control).

因此​​我实现了一个新创建的服务器控件的渲染方法。新创建的控制距离的ValidationSummary继承。

therefore i implemented the render method of a new created server control. the new created control inherits from ValidationSummary.

public class AgValidationSummary : ValidationSummary
{
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        if(this.Enabled)
        {
            if (Page.IsPostBack && !Page.IsValid)
            {

我的问题是,现在,如果一个按钮被解雇,他的属性CausesValidation设置为false,因为我问Page.IsValid属性我的ValidationSummary将抛出一个异常(而这仅是设置是否有页的通话.validate()。

my problem is now, that if an button is fired and his property CausesValidation is set to false my validationsummary will throw an exception because i ask about the Page.IsValid property (and this is only set if there was an call to Page.validate().

有人来过了解决问题的办法?

has somebody a solution for the problem?

推荐答案

解决方案是某种简单:结果
只是不解析 Page.IsValid 标记:),而不是做这样的事情,在你的无效渲染(HtmlTextWriter的作家)

the solution is somehow easy:
just don't parse the Page.IsValid flag :) instead do something like that in your void Render(HtmlTextWriter writer):

if (!this.Enabled)
{
    return;
}

if (this.Page != null)
{
    this.Page.VerifyRenderingInServerForm(this);
}
else
{
    return;
}

var failedValidators = this.Page.Validators.OfType<BaseValidator>().Where(bv => string.Equals(bv.ValidationGroup, this.ValidationGroup) && !bv.IsValid).ToList();

if (failedValidators.Any())
{
    writer.Write(this.HeaderText);
    foreach (var failedValidator in failedValidators)
    {
    	writer.Write(failedValidator.ErrorMessage);
    }
}

为什么这个工作的原因:结果
控制,这会导致回传,得到了约

The reason why this works:
The control, which causes the postback, has got the information about


  1. 的CausesValidation

  2. 的ValidationGroup

  3. 和其他的东西

所以ASP.net引擎本身将执行相关的验证,并将其设置为的IsValid 或没有。

So the ASP.net-engine itself will execute the associated validators and set them to IsValid or not.

修改结果
为了摆脱原有的HeaderText(仍然呈现)的:

edit
To get rid of the original HeaderText (which is still rendered):

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
    // overwrite to hide the original HeaderText
}

这篇关于从ASP继承:的ValidationSummary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆