中继器控制中的现场验证

时间:2012-02-28 19:20:12

标签: asp.net vb.net textbox repeater customvalidator

我有一个与asp.net和vb.net相关的问题。我有一个转发器控件,我绑定一些数据,并允许用户更新/更改某些文本框中的字段。我添加了一个验证控件,当用户没有输入有效日期或文本“TBD”时触发。在提交时,我想通过并突出显示其相应验证器无效的每个字段。这是我目前的代码,但我对如何找到文本框控件感到很遗憾。

Sub ValidateDateField(ByVal sender As Object, _
                      ByVal args As ServerValidateEventArgs)
    'validate against three conditions - date, "TBD", and "N/A"
    Dim dtValue = args.Value
    If dtValue.ToUpper = "TBD" Or dtValue.ToUpper = "N/A" Then
        args.IsValid = True
    ElseIf IsDate(dtValue) Then
        args.IsValid = True
    Else
        args.IsValid = False

        Dim cont As WebControl = DirectCast(Page.FindControl(args.ToString), WebControl)

        cont.BackColor = Drawing.Color.White

        util.Client_Alert("Please Update Highlighted Fields")

    End If
End Sub

我完全迷失了如何获得我的转发器控件的cont = textbox1row1。请指教。到目前为止我看到的所有示例都直接在text1.BackColor =

中声明控件

1 个答案:

答案 0 :(得分:0)

所以我想出了答案。对不起,这个问题。不知道我为什么不早点考虑。我找到了我的自定义val的命名容器,然后根据转发器中的名称找到了控件。在vb.net中它是这样的:

Sub ValidateDateField(ByVal sender As Object, _
                      ByVal args As ServerValidateEventArgs)

    'args holds value. validate against three conditions - date, "TBD", and "N/A"
    Dim dtValue = args.Value

    Dim cont As CustomValidator = sender
    Dim myNC As Control = cont.NamingContainer    
    Dim strControl As String

    strControl = cont.ControlToValidate

    Dim txtbox As TextBox = _
      DirectCast(myNC.FindControl(cont.ControlToValidate), TextBox)

    If dtValue.ToUpper = "TBD" Or dtValue.ToUpper = "N/A" Or IsDate(dtValue) Then
        args.IsValid = True
        txtbox.BackColor = Drawing.Color.Empty
    Else
        args.IsValid = False

        txtbox.BackColor = Drawing.Color.White

    End If

End Sub