动态必需字段验证器

时间:2012-04-23 06:45:09

标签: asp.net

我正在创建动态控件,我想添加必需的字段验证器,下面是我的代码,但是必需的字段验证器没有触发。我在page_init中添加控件。任何人都知道为什么这段代码不起作用。当我添加单个控件时,同一段代码正在工作。当我试图添加多行控制时它不起作用。

ctrls.Add(New LiteralControl("<br />"))
                ctrls.Add(CreateRequiredFieldValidatorMR(StrControlID.ToString, "Required field."))


Private Function CreateRequiredFieldValidatorMR(ByVal strControlID As String, ByVal ErrorMessage As String) As RequiredFieldValidator
    Return CreateRequiredFieldValidatorMR(strControlID, ErrorMessage, String.Empty)
End Function

Private Function CreateRequiredFieldValidatorMR(ByVal strControlID As String, ByVal ErrorMessage As String, ByVal InitialValue As String) As RequiredFieldValidator
    Dim rfv As New RequiredFieldValidator
    rfv.ID = "ReqVal_" & strControlID
    rfv.ControlToValidate = strControlID
    rfv.Display = ValidatorDisplay.Dynamic
    rfv.ErrorMessage = ErrorMessage
    rfv.InitialValue = InitialValue
    rfv.ValidationGroup = "ReqGrp_" + strControlID
    rfv.ForeColor = Drawing.Color.Red
    rfv.Font.Bold = True
    Return rfv
End Function

0 个答案:

没有答案