在回发,我怎么可以添加一个错误消息验证摘要? [英] On postback, how can I add a error message to validation summary?

查看:132
本文介绍了在回发,我怎么可以添加一个错误消息验证摘要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题:

在回发,当用户点击提交,我怎么可以添加一个错误消息验证摘要?

On postback when a user clicks submit, how can I add a error message to validation summary?

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

Is it also possible to highlight a particular textbox using the built in .net validation controls?

推荐答案

动态创建CustomValidator控件,并直接将它添加到收藏Page.Validators。

Dynamically create a CustomValidator control and add it directly to the Page.Validators collection.

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

与添加的CustomValidator的标记,这种方法允许你添加任意数量的基于服务器端的业务逻辑的任意错误消息。

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. 您必须将控件添加到相同的命名容器作为验证组的控件。

  2. 如果您不希望出现在页面上的随机位置的确认消息,您将必须验证器添加到特定容器,否则将使用一个CSS类需要苏preSS,或风格。

您还可以创建一个<一个href=\"http://blog.webmastersam.net/post/Adding-custom-error-message-to-ValidationSummary-without-validators.aspx\">custom类并实现IValidator ,使您能够以code的一行添加的消息,但这种方法不支持验证组。

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.

每安德斯Fjeldstad的建议,这里是一组方便的扩展方法。

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

这篇关于在回发,我怎么可以添加一个错误消息验证摘要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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