Binding Business Object on Textbox Control?Binding Business Object on Textbox Control?
Old forum URL: forums.lhotka.net/forums/t/985.aspx
brembot posted on Wednesday, August 23, 2006
I have encountered a problem when binding my object to a textbox. What happened is when i edit the textbox and press TAB control for the next textbox, you have pressed it twice. I tried to trace the problem and found out that ValidationRules that cause the problem. When i removed it and it's working fine. You don't have to press the TAB twice in order to go the next control.
Have anyone has an idea how to resolve this issue co'z i would like some of my properties have ValidationRules?
Thanks
ajj3085 replied on Wednesday, August 23, 2006
It sounds like something in your validation rules might be throwing an exception. What are your rules? What does your property setter look like for the property which is bound to the text box?
RockfordLhotka replied on Wednesday, August 23, 2006
It is likely that your code is throwing an exception - at least on the first TAB - and this exception causes data binding to lock the user on the control. Presumably that first TAB exception is somehow resolved before the second TAB is pressed (it is probably a null reference exception or something) and so the second TAB works.
The root of the issue is that data binding swallows/hides these exceptions, making debugging difficult. You'll have to put a debug breakpoint in the property set code and step through it to find the exception so you can resolve it.
brembot replied on Wednesday, August 23, 2006
Hello Rocky,
I've fixed the problem by replacing the PropertyHasChanged to MarkDirty. I know that PropertyHasChanged call these methods MarkDirty, CheckRules and OnPropertyChanged.
These are my codes:
......
Public Property Address() As String
Get
CanReadProperty(True)
Return mAddress
End Get
Set(ByVal value As String)
CanWriteProperty(True)
If Not mAddress.Equals(value) Then
mAddress = value
PropertyHasChanged()
End If
End Set
End Property
Public Property TelNo() As String
Get
CanReadProperty(True)
Return mTelNo
End Get
Set(ByVal value As String)
CanWriteProperty(True)
If Not mTelNo.Equals(value) Then
mTelNo = value
PropertyHasChanged()
End If
End Set
End Property
Public Property FaxNo() As String
Get
CanReadProperty(True)
Return mFaxNo
End Get
Set(ByVal value As String)
CanWriteProperty(True)
If Not mFaxNo.Equals(value) Then
mFaxNo = value
PropertyHasChanged()
End If
End Set
End Property
....
Protected Overrides Sub AddBusinessRules()
'// TODO: add validation rules
ValidationRules.AddRule(AddressOf CommonRules.StringRequired, "Address")
ValidationRules.AddRule(AddressOf CommonRules.StringMaxLength, New CommonRules.MaxLengthRuleArgs("Address", 50))
End Sub
What happened is this... As you have notice that i only have one ValidationRules and that's for Address. When i try to modify the Address (bounded textbox) and press TAB or Position the mouse to the next control, it works fine because i have implemented a ValidationRules for this property but for those properties that doesn't have ValidationRules. I notice that when change the focus to the next control it will try to validate just because i implemented the PropertyHasChanged. In that case, i have to press TAB or Position the mouse twice to get the focus of the next control.
Any ideas how to solve this issue Or do we have to implement MarkDirty, CheckRules separately just like we did in CSLA 1.53?
Thanks
xal replied on Wednesday, August 23, 2006
Any chance you're using the first release of 2.1 that rocky made some time ago? That one had that problem because it was throwing null reference exceptions sometimes when attempting to validate.
In any case, adding an error provider to the form and binding it to the binding source, might show you what the problem is...
Andrés
brembot replied on Wednesday, August 23, 2006
Hello Andrés,
You are right... I'm using the first release of CSLA 2.1. Maybe i will to use the previous version CSLA 2.0.3 to see the difference. Thanks for the info...
Cheers,
Marlon
Copyright (c) Marimer LLC