通过Code Behind创建RequiredFieldValidator

时间:2009-10-31 12:06:07

标签: asp.net asp.net-ajax

我想通过代码隐藏创建验证。但是虽然我创建了它,但是当回发发生时它不会触发。我在Page的Onload事件中创建验证器。可能是什么原因?这是我的代码。

Public Sub Validate(ByVal p_Page As Page, ByRef p_AssignedControl As System.Web.UI.WebControls.WebControl, ByVal p_Message As String)

    Dim v_VCALLOUT As New AjaxControlToolkit.ValidatorCalloutExtender

    Dim r_VALIDATOR As New System.Web.UI.WebControls.RequiredFieldValidator
    Dim placeHolder As PlaceHolder = p_Page.FindControl("Vld")


    With r_VALIDATOR

        .ID = "r_" & p_AssignedControl.ID
        .ControlToValidate = p_AssignedControl.ID
        .Display = ValidatorDisplay.None
        .ErrorMessage = p_Message
        .Font.Name = "Verdana"
        .Font.Size = FontSize.XSmall

    End With

    With v_VCALLOUT

        .ID = "v_" & r_VALIDATOR.ID
        '.CssClass = "highlight"
        .HighlightCssClass = "highlight"
        .WarningIconImageUrl = "~/images/warning.gif"
        .CloseImageUrl = "~/images/close.gif"
        .TargetControlID = r_VALIDATOR.ID
        .Enabled = True

    End With

    placeHolder.Controls.Add(v_VCALLOUT)
    placeHolder.Controls.Add(r_VALIDATOR)


End Sub

2 个答案:

答案 0 :(得分:0)

首先想到:(来自MSDN)

无验证消息永远不会内联显示。

您是否有验证摘要?

第二个想法:

尝试在Page_Init事件中创建验证器。

答案 1 :(得分:0)

我找到了理由。如果page.IsPostpact = False,我会调用Validate Sub。如果我在每次回发中调用Validate Sub,它的效果都很好。