在回发时,如何向验证摘要添加错误消息?

Bla*_*man 57 asp.net validation

两个问题:

在用户单击提交时回发时,如何向验证摘要添加错误消息?

是否也可以使用内置的.net验证控件突出显示特定的文本框?

Nig*_*888 87

动态创建CustomValidator控件并将其直接添加到Page.Validators集合.

Dim err As New CustomValidator
err.ValidationGroup = "MyGroup"
err.IsValid = False
err.ErrorMessage = "The password is invalid"
Page.Validators.Add(err)
Run Code Online (Sandbox Code Playgroud)

Unlike adding the CustomValidator to the markup, this method allows you to add any number of arbitrary error messages based on server-side business logic.

Note that you can also add it to the page directly, but there are a couple of rules to follow:

  1. You must add the control to the same naming container as the controls of the validation group.
  2. If you don't want the validation message to appear in a random position in the page, you will either have to add the validator to a specific container or you will need to supress it using a CSS class or style.

You can also create a custom class and implement IValidator, which enables you to add the message with one line of code, but this method doesn't support Validation Groups.

Per Anders Fjeldstad's suggestion, here is a set of handy extension methods.

Imports Microsoft.VisualBasic
Imports System.Runtime.CompilerServices

Public Module PageExtensions

    <Extension()> _
    Public Sub AddValidationError(ByVal p As System.Web.UI.Page, ByVal errorMessage As String)
        p.AddValidationError(errorMessage, String.Empty)
    End Sub

    <Extension()> _
    Public Sub AddValidationError(ByVal p As System.Web.UI.Page, ByVal errorMessage As String, ByVal validationGroup As String)
        Dim err As New CustomValidator
        err.ValidationGroup = validationGroup
        err.ErrorMessage = errorMessage
        err.IsValid = False
        p.Validators.Add(err)
    End Sub

End Module
Run Code Online (Sandbox Code Playgroud)

  • +1用于启用任意数量的错误消息.另外,上面的代码很容易被分解为System.Web.UI.Page或System.Web.UI.ValidatorCollection的扩展方法,这将通过类似Page.AddValiationError("MyValidationGroup","Some some error"信息") (4认同)

use*_*794 48

添加自定义验证器并手动设置它IsValidErrorMessage属性.有点像:

<asp:panel ID="ErrorsPanel" runat="server" CssClass="ErrorSummary">
    <asp:CustomValidator id="CustomValidator1" runat="server" 
        Display="None" EnableClientScript="False"></asp:CustomValidator>
    <asp:ValidationSummary id="ErrorSummary" runat="server" 
        HeaderText="Errors occurred:"></asp:ValidationSummary>
</asp:panel>
Run Code Online (Sandbox Code Playgroud)

在后面的代码中:

//
// Update the database with the changes
//
string ErrorDetails;
if (!Db.Update(out ErrorDetails))
{
    CustomValidator1.IsValid = false;
    CustomValidator1.ErrorMessage = ErrorDetails;
}
Run Code Online (Sandbox Code Playgroud)

  • 这里的一个陷阱是,如果验证摘要控件指定了ValidationGroup,那么您还需要将ValidationGroup属性添加到CustomValidator,以确保在需要时显示错误消息.也许只是常识但它偶尔会把我抓住. (5认同)

Say*_*yan 6

那么您需要做的就是创建一个自定义验证器并将其添加到页面的验证器集合中,只要有条件就可以这样做。

CustomValidator cv = new CustomValidator();

if(condition)
{
cv.IsValid = false;
cv.ErrorMessage = "This Catalog Data already exists.";
cv.ValidationGroup = "InputList";
this.Page.Validators.Add(cv);
}
Run Code Online (Sandbox Code Playgroud)

注意:不要忘记指定ValidationGroup,否则不会显示错误消息,尽管将自定义验证器添加到您的页面。是的,如果您确实得到了第二个问题的答案(突出显示文本框),请发布它!


atl*_*ste 6

这是NightOwl888好主意的一点延伸:

public class ValidationError : CustomValidator
{
    public ValidationError(string group, string msg)
        : base()
    {
        base.ValidationGroup = group;
        base.ErrorMessage = msg;
        base.IsValid = false;
    }
}

public static class PageExtensions
{
    public static void ErrorMessage(this Page page, string group, string msg)
    {
        page.Validators.Add(new ValidationError(group, msg));
    }
}
Run Code Online (Sandbox Code Playgroud)

每当您想发出错误消息时,只需致电Page.ErrorMessage; 机制与他的建议相同.


Can*_*var 3

要在验证摘要上添加错误消息,您可以使用ValidationSummary 和其他验证控件的EnableClientScript属性。将所有 EnableClientScript 设置为 false :

<asp:ValidationSummary
HeaderText="You must enter a value in the following fields :"
DisplayMode="BulletList"
EnableClientScript="false"
runat="server"/>
Run Code Online (Sandbox Code Playgroud)

对于突出显示控件,目前的控件是不可能的。

但我将验证控件放在相关控件附近,并将它们的Text属性设置为“*”。然后,如果验证失败,它就会出现接近失败的控制。

也许您可以使用自定义验证器来突出显示失败的控件。但你应该编写自己的实现。