使用c#编辑验证摘要

时间:2015-12-03 17:48:50

标签: javascript c# asp.net validation customvalidator

我想创建一个自定义验证摘要,以便我可以编辑asp:ValidationSummary的默认呈现,其中包含指向特定错误字段的链接(有点像WET)。

我的自定义类:

    using System;
    using System.ComponentModel;
    using System.Security;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [assembly: TagPrefix("Web.Boew", "wet")]
     namespace Wet.Boew
      {

    [
        ToolboxData("<{0}:CustomValidationSummary CssClass='label label-danger' ID='ValidationSummary' runat=\"server\"></{0}:CustomValidationSummary>")
    ]
    public class CustomValidationSummary : System.Web.UI.WebControls.ValidationSummary
    {
        [
            Browsable(true),
            Category("Appearance"),
            DefaultValue(""),
            Description("Validation Summary.")
        ]

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "label label-danger");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            // Call the base class's Render method.
            base.Render(writer);

            foreach (BaseValidator v in Page.Validators)
            {
                //base.Render(writer);

                var o = v.FindControl(v.ControlToValidate);
                if (o != null && v.ValidationGroup.Equals(this.ValidationGroup))
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "#" + o.ClientID);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write(o.ToString());
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                }
            }
            writer.RenderEndTag();
            writer.RenderEndTag();

        }
    }
}

代码:

    <wet:CustomValidationSummary ID="ValidationSummary2" runat="server"  
      ValidationGroup="Registration" HeaderText="Following errors occured:" />

呈现HTML: enter image description here

发生了什么: 如果你看到上面的图像。加载页面时,它会预先加载System.Web.UI.WebControls.TextBox列表,该列表可单击到文本字段。哪个是对的。但是当我按提交时,我会看到上面的错误列表由base.Render(writer)生成。

我想要的是什么:

我想编辑base.Render(writer)生成的列表。并编辑其HTML以包含指向有错误的文本字段的链接。如果有人可以帮助对不起我是asp和C#的新手。

最终输出我的愿望: enter image description here

0 个答案:

没有答案