2011-12-22 8 views
0

私はasp.net Webフォームで、検証が必要なコントロールがたくさんあります。検証を必要とするコードビハインドページの各コントロールをリストするのではなく、必要なコントロールにcssクラス "required"を追加して、コードを送信すると、このクラスの各コントロールが検証されます。特定のCSSクラスを含むasp.net(VB)パネルのコントロールを検証します。

Public Sub ValidateAllControls(ByVal a As Object) 
     For Each c As Control In a.Controls 
      If c.Controls.Count > 0 And c.Controls.class = "required" Then 
       ValidateAllControls(c) 
      Else 
       If TypeOf c Is TextBox Then 
        If CType(c, TextBox).Text = "" Then 
         c.Controls.BorderColor = Drawing.Color.Red 
         lblValidate.text = "Text Box cannot be empty" 
        End If 
       End If 
      End If 
     Next 
    End Sub 

ご提案を聞いて幸せ:私はいくつかの擬似コードであるので、ここでは.NETにかなり新しいです。ありがとう

答えて

0

私はそれを理解しました。あなたの[送信]ボタンを、あなたのコントロールにToolTip="required"を追加することを確認の上ValidateAllControls(Me, Me)

Public Sub ValidateAllControls(ByVal a As Object, FormControl As Control) 
     For Each c As Control In a.Controls 
      If c.Controls.Count > 0 Then 
       ValidateAllControls(c, FormControl) 
      Else 
       If TypeOf c Is TextBox Then 
        Dim tb As TextBox = CType(c, TextBox) 
        If tb.ToolTip = "required" And tb.Text.Length = 0 Then 
         tb.BackColor = Drawing.Color.Red 
         tb.BorderColor = Drawing.Color.Black 
         tb.BorderStyle = BorderStyle.Solid 
         tb.BorderWidth = "1" 
         Dim lbl As Label = FormControl.FindControl("lblvalidate") 
         lbl.Text &= "The fields in red cannot be empty!" 
        End If 
       End If 
      End If 
     Next 
    End Sub 

コール:これは私がそれを望んでいたどのように動作します。お役に立てれば。

関連する問題