继承自 asp:validationsummary

发布于 2024-07-16 06:42:25 字数 524 浏览 10 评论 0原文

我需要在页面(或控件)顶部显示格式良好的错误消息。

因此我实现了新创建的服务器控件的渲染方法。 新创建的控件继承自ValidationSummary。

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

我现在的问题是,如果一个按钮被触发并且他的属性 CausesValidation 设置为 false 我的validationsummary 将抛出异常,因为我询问 Page.IsValid 属性(并且只有在调用 Page.validate( 时才会设置此属性) )。

有人解决这个问题吗?

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

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)
            {

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹊巢 2024-07-23 06:42:25

解决方案很简单:
只是不要解析 Page.IsValid 标志:)而是在您的 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);
    }
}

这样做的原因:
导致回发的控件已获取有关

  1. CausesValidation
  2. ValidationGroup
  3. 和其他内容

的信息,因此 ASP.net-engine 本身将执行关联的验证器并将它们设置为 IsValid 或不设置。

编辑
要删除原始的 HeaderText(仍然呈现):

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

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. and other stuff

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

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

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
    // overwrite to hide the original HeaderText
}
む无字情书 2024-07-23 06:42:25

也许你可以将 IsValid 属性保存在 ViewState 中,并将其初始化为 true。

在Load中检查IsValid是否为null,如果不为null,则将你ViewState中的IsValid设置为Page.IsValid中的值。

在 Render 中,从 ViewState 读取 IsValid。

Maybe you can save IsValid property in ViewState, and initialize it to true.

In Load, check if IsValid is null, if not null, set your IsValid in ViewState to the value in Page.IsValid.

And in Render, read IsValid from ViewState.

只涨不跌 2024-07-23 06:42:25

我可能偏离了轨道,但是您不能在控件的构造函数中将其连接到页面的 Validate 事件吗?

如果需要,您可以有一个内部标志,渲染器会检查该标志以查看是否执行渲染代码中接下来发生的任何操作。

private bool _thePageIsBeingValidated = false;

public bool ShouldIDoMyThing
{
     get{ return (_thePageIsBeingValidated && this.Enabled && this.Page.IsPostback && this.Page.IsValid != null && this.Page.IsValid == false); }
}

I may be off track here, but can't you just wire up in your control's constructor to the page's Validate event?

If needed, you can have an internal flag that the render checks to see whether to do whatever happens next in your render code.

private bool _thePageIsBeingValidated = false;

public bool ShouldIDoMyThing
{
     get{ return (_thePageIsBeingValidated && this.Enabled && this.Page.IsPostback && this.Page.IsValid != null && this.Page.IsValid == false); }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文