I have a requirement for a form to support view/edit behavior. In other words, the form is initially in "view" mode and the user must click a button to enter "edit" mode. Once in "edit" mode, the user can "save" or "cancel" their changes to return to "view"mode etc.
There are a couple of ways to do this using panels and such but I was wondering if anybody has tried to work this at a control level, factoring in the CSLA authorization behavior, without battling for who is managing each control's Enabled and ReadOnly properties. Maybe I should forgo using CSLA authorization at the control level (no ReadWriteAuthorization extender)?
I am new to CSLA and learning its structure so I may be missing an obvious place/way to plug/build this functionality in.
I implemented a scheme in 1.x before the current authorization roles scheme came out whereby I have a function on my business object called FieldEditable where I pass the field name in and find out if it is editable or not. In my forms, I create a collection which helps to manage the databindings. I can then call a method to set visible based on the object's editable status and it loops through the control collection setting the enabled property on a per-field basis depending on the FieldEditable value for that field. This was necessary for 2 purposes: As my objects change values (dates) the state changes which changes which fields are then editable, also I had a business rule which allowed for certain users to be able to override values regardless of state.
It is a rather flexible model. I didn't go to the level of checking for FieldEditable on each property set, but that would probably be a bit more robust. I have yet to ponder how it would fit with the new scheme in 2.0 as I'm still stuck in 1.x world having made customizations to the framework and not having the time to retrofit them into 2.x at this point.
Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Thanks Xal. Putting it all together, this seems to work pretty good. Here is what I did. I put this in my BO:
public override bool CanWriteProperty(string propertyName)
{
if (this.EditLevel > 1)
return base.CanWriteProperty(propertyName);
else
return false;
}
Add a ReadWriteAuthorization control to the form and set the applicable data entry controls to ApplyAuthorization. Then when I want to enter "Edit" mode, I do a BeginEdit() followed by a call to:
readWriteAuthorization.ResetControlAuthorization();to refresh/enable the data entry controls. After I ApplyEdit() and save or CancelEdit(), I call ResetControlAuthorization() again to refresh/disabled the controls.
Although I am not using Authorization roles right now, they should still take effect so proper authorization should remain intact. I think I will consider moving the CanWriteProperty() code above to an abstract class off BusinessBase for this application so similiar BOs can share this behavior.
Thanks to everyone who chimed in!
ajj3085, I actually am using ResetControlAuthorization() of the ReadWriteAuthorization control to re-evaluate. Does that make sense?
Thanks for your help.
Copyright (c) Marimer LLC