CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Disable property based on another property value

Last post 09-18-2008, 9:51 AM by ajj3085. 9 replies.
Sort Posts: Previous Next
  •  09-05-2008, 1:59 PM 25978

    Disable property based on another property value

    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

  •  09-05-2008, 2:58 PM 25981 in reply to 25978

    Re: Disable property based on another property value

    Yes, you can override CanWriteProperty to perform this kind of logic.
  •  09-05-2008, 3:13 PM 25982 in reply to 25981

    Re: Disable property based on another property value

    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

  •  09-07-2008, 7:23 AM 26002 in reply to 25978

    Re: Disable property based on another property value

    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)
  •  09-07-2008, 10:24 AM 26003 in reply to 25982

    Re: Disable property based on another property value

    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);
    }
  •  09-07-2008, 12:42 PM 26005 in reply to 26003

    Re: Disable property based on another property value

    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.

  •  09-08-2008, 5:51 AM 26011 in reply to 25978

    Re: Disable property based on another property value

    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
  •  09-12-2008, 10:13 AM 26173 in reply to 26011

    Re: Disable property based on another property value

    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

  •  09-18-2008, 8:32 AM 26355 in reply to 26173

    Re: Disable property based on another property value

    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

  •  09-18-2008, 9:51 AM 26369 in reply to 26355

    Re: Disable property based on another property value

    Yes, you need to call the ResetAuthorization on the RWA component whenever the permissions may have changed.
View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems