Αυτά που κάνεις μπορούν να γίνουν πολύ ευκολότερα με τις λειτουργίες που υπάρχουν ήδη στο .NET. Για παράδειγμα, μπορείς να χρησιμοποιήσεις την String.Format για να δημιουργήσεις οποιοδήποτε string με το format που θέλεις, χωρίς να σε απασχολούνε τα , και τα . . Για παράδειγμα, μπορείς με ένα text = String.Format("{0:C}", numberValue) να μετατρέψεις οποιαδήποτε τιμή σε string με currency format, δηλαδή με 2 δεκαδικά και το σύμβολο νομίσματος που έχει ορίσει ο χρήστης. Αν θέλεις να λάβεις υπόψη και τα null, κάνεις το
Dim aValue As Object
aValue = DBNull.Value
Dim numberValue As Object = IIf(IsDBNull(aValue), 0, aValue)
Dim text As String = String.Format("{0:C}", numberValue)
Όσον αφορά την FindControl, υπάρχει ήδη FindControl. Στην LockDown, δεν χρειάζονται όλες αυτές οι μετατροπές καθώς το Enabled property ορίζεται στην WebControl κλάση από την οποία κληρονομούνε όλες οι άλλες. Απλά γράφεις ένα
Dim wControl As WebControl = TryCast(aControl, WebControl)
If Not wControl Is Nothing Then
wControl.Enabled = False
End If
Μπορείς να πιάσεις και τα HtmlControls ως εξής
Private Sub CascadeEnable(ByVal root As Control, ByVal enabled As Boolean)
Dim wControl As WebControl = TryCast(root, WebControl)
Dim hControl As HtmlControl = TryCast(root, HtmlControl)
If Not wControl Is Nothing Then
wControl.Enabled = enabled
ElseIf Not hControl Is Nothing Then
hControl.Disabled = Not enabled
End If
For Each child As Control In root.Controls
CascadeEnable(child, enabled)
Next
End Sub
Ακόμα καλύτερα, μπορείς να χρησιμοποιήσεις delegates για να φτιάξεις μία IterateControl η οποία θα εκτελεί σε κάθε control τον κώδικα που θα δώσεις εσύ
Delegate Sub ControlFlagDelegate(ByVal aControl As Control, ByVal flag As Boolean)
Private Sub IterateControl(ByVal root As Control, ByVal code As ControlFlagDelegate, ByVal flag As Boolean)
code(root, flag)
For Each child As Control In root.Controls
IterateControl(child, code, flag)
Next
End Sub
Private Sub EnableControl(ByVal aControl As Control, ByVal enabled As Boolean)
Dim wControl As WebControl = TryCast(aControl, WebControl)
Dim hControl As HtmlControl = TryCast(aControl, HtmlControl)
If Not wControl Is Nothing Then
wControl.Enabled = enabled
ElseIf Not hControl Is Nothing Then
hControl.Disabled = Not enabled
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
IterateControl(Me, AddressOf Me.EnableControl, False)
End Sub
Παναγιώτης Καναβός, Freelancer
Twitter: http://www.twitter.com/pkanavos