Friday, 9 August 2013

How to disallow editing in a DynamicField?

How to disallow editing in a DynamicField?

pI've very recently started using a
href=http://msdn.microsoft.com/en-us/library/ee845452%28v=vs.100%29.aspx
rel=nofollowDynamic
Data/a features on a few pages of our WebForms
project. Since this is not a new project, I'm doing a slow conversion
process to avoid breaking things. At the moment, I'm just changing a few
fields in a a href=http://msdn.microsoft.com/library/s3w1w7t4.aspx
rel=nofollowDetailsView/a
from a custom a
href=http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.aspx
rel=nofollowTemplateField/a
to a DynamicField control and annotating the
model class. Even thought it is working wonderfully for the most part, I'm
having a hard time trying to make the a
href=http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.dynamicfield.aspx
rel=nofollowDynamicField/a
display in codeReadOnly/code mode for the
primary key of my model class./p pEntityFramework reports an error if you
try to change the value of a key property in the model, which is expected.
I thought Dynamic Data would handle this by automatically disallowing the
user to edit the field in case it is a key property./p pAt first, I did
not have the a
href=http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.keyattribute.aspx
rel=nofollow[Key]/a
attribute on the offending property, but after adding
it nothing changed (the field is still editable). After searching a bit on
the a
href=http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx
rel=nofollowDataAnnotations
namespace/a I found other attributes which I
thought would enable this behavior (a
href=http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.editableattribute.aspx
rel=nofollow[Editable]/a
and a
href=http://msdn.microsoft.com/en-us/library/system.componentmodel.readonlyattribute.aspx
rel=nofollow[ReadOnly]/a
attributes), but none of them worked as
expected./p pMy current workaround is to use a TemplateField and put a a
href=http://msdn.microsoft.com/en-us/library/system.web.dynamicdata.dynamiccontrol.aspx
rel=nofollowDynamicControl/a
with codeMode=ReadOnly/code in the
EditTemplate. I don't like this approach because I'm losing the automatic
codeHeaderText/code of DynamicField and obviously because it is a lot more
verbose and error prone./p pIs there a way to make the DynamicField
readonly when in EditMode? Maybe an attribute I'm missing? Why isn't this
the standard behavior for primary key properties? This is the property
with all the attributes applied:/p precode[Required(ErrorMessage = The
unit Code is mandatory.)] [Display(Name = Unit Code)] [Key]
[Editable(false, AllowInitialValue=true)] public string Code { get; set; }
/code/pre pAlso, this is a .Net4/Visual Studio 2012/EntityFramework 5
environment./p

No comments:

Post a Comment