How to conditionally make a DetailsView field read-only?
Say I have a DetailsView
with a bunch of fields, and I want only certain kinds of users to edit a few of them. They're too few to split them into another DetailsView
, so what I'm thinking is to find some way to only allow a user to edit them based on some code-behind logic, effectively making them read-only at will.
I feel it's important to mention that the fields are both TemplateField
s, not normal BoundField
s with ReadOnly
properties.
Any ideas? For some reason the required functions don't jump at me from 开发者_StackOverflow中文版reading the documentation.
Oh and I need eveyone to see their specific values, I just want to restrict edit access to them.
Hrm apparently it was as simple as setting the EditItemTemplate
property of the fields in question to null
. Seems to be working fine so far!
Edit: A short code sample showing how I did it:
foreach (DataControlField field in dvDRDetails.Fields)
if (!fieldstoignore.Contains(field.HeaderText))
if (field is TemplateField)
((TemplateField)field).EditItemTemplate = null;
else if (field is BoundField)
((BoundField)field).ReadOnly = true;
Where fieldstoignore
is an array of field headers that I always have set as editable. The rest fall in two categories: TemplateField
that require the hack I discussed above and BoundField
that have a ReadOnly
property I can set.
精彩评论