Disable property based on another property value

Disable property based on another property value

Old forum URL: forums.lhotka.net/forums/t/5352.aspx


jfreeman posted on Friday, September 05, 2008

Based on the newest version of the framework, is there a way to disable or prevent changes to a property based on the value of another property?  For instance, if property 1 has the value of 2000, I do not want the user to be able to update property 2.  Can this be handled in the BO or should the UI handle it?  Thanks.

Jonathan

ajj3085 replied on Friday, September 05, 2008

Yes, you can override CanWriteProperty to perform this kind of logic.

jfreeman replied on Friday, September 05, 2008

I've not had to use the CanWriteProperty before.  Can you give an example of how it would be used in this situation?  Thanks.

Jonathan

triplea replied on Sunday, September 07, 2008

Probably the 2 business rules way (as mentioned above) would be better since you could provide an error/warning which describes why a field should not be edited and prevent saving.
But overriding CanWriteProperty is fine. As an example, override CanWriteProperty and within it have something like:

switch (propertyName)
{
    case "2": return !(this.1 == 2000);
}

Biceman replied on Sunday, September 07, 2008

I tackled this problem myself recently.  My approach was to create a ReadOnly boolean property in the BO that I bound to my textbox's "enabled" property.  In the "Set" code for the controlling property (the one you test =2000), I adjust the value of the ReadOnly property accordingly.

Using this approach, the user is not allowed to enter data.  I prefer this to allowing them to enter data then having them receive an error message saying there are not allowed to enter data.

rsbaker0 replied on Sunday, September 07, 2008

You can also do this with a standard business rule, depending on what behavior you are looking for. The business rule would be broken if property 2 was changed while property 1  has a value of 2000, and pass otherwise. (This would require that you know the original value of property 2, to allow the rule to be unbroken by putting the value back to the original value)

JonnyBee replied on Monday, September 08, 2008

I'd prefer to use the CanWriteProperty/CanReadProperty methods and then use the ReadWriteAuthorizationManager in CSLA on my WinForms. The RWAM will enable/disable/set readonly on UI Controls based on the result from CanWrite... and CanRead...

JonnyB

jfreeman replied on Friday, September 12, 2008

Overriding the CanWriteProperty works when the value is static.  So if property 1 is 2000, property 2 does get disabled using the override and the ReadWriteAuthorizationManager.  However, when the user changes the value of property 1 to something besides 2000, they should then be able to update property 2.  I would have to reset the RWAM everytime I change a field to make this work.  Is there a better way to handle this situation.  I hate to have to keep resetting the RWAM that much.  Thanks.

Jonathan

jfreeman replied on Thursday, September 18, 2008

Do I have to reset the ReadWriteAuthorizationManager when I have values that are dependent on other properties and those properties can be changed by the user?  For instance:

Property 1 is 2000, property 2 gets disabled using the CanWriteProperty override and the ReadWriteAuthorizationManager.  However, when the user changes the value of property 1 to something besides 2000, they should then be able to update property 2.  To make this happen I would have to reset the ReadWriteAuthorizationManager right?

Jonathan

ajj3085 replied on Thursday, September 18, 2008

Yes, you need to call the ResetAuthorization on the RWA component whenever the permissions may have changed.

Copyright (c) Marimer LLC