2012-02-28 42 views
0

我有一個與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。請指教。到目前爲止我看到的所有例子都直接說明了文本中的控件.BackColor =

+0

所以我得到這個工作,可能是一個笨拙的方式來做到這一點,但我不能後驗證。我必須等待8個小時。當我被允許時會重新發布。 – user1165864 2012-02-28 19:59:47

回答

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