Is it possible to make a field "un-editable" without having to hide it using Dynamic Data? I would like a field to be displayed to the user, but I don't want them to be able to edit it. Is there something I can do in the Custom Edit.aspx or Partial Class
for that table?
Linq to SQL hides PK, but you can display them with [ScaffoldColumn(true)]
If you annotate the entity partial class to display the PK and edit an entity, the PK is displayed read-only. That looks like the behavior you want. I'll find out how Dynamic Data knows to make the PK R/O.
One approach would be to copy DynamicData\FieldTemplates\Text.ascx to TextRO_Edit, then annotate you partial class with a UIHInt to use TextRO. On edit, you wouldn't get the edit text box, but the literal.
I think your idea on creating a TextRO_Edit user control would do the trick. The field I want displayed - but not editable is not a primary key, it is just a regular field the user would like to see. They just don't want it to be edited.
Would you happen to know how to set the UIInt to use this TextRo user control in the partial class? I am using Dynamic Data Entity Framework with Visual Basic. My partial class for this table is shown below. Branch is the field I want to have displayed,
but not editable. Thanks.
GetType(CapitalAssetsMetaData))> _
Partial
Public
Class CapitalAssets
End
Class
Public
Class CapitalAssetsMetaData <ScaffoldColumn(False)>
_
Public CapitalAssetID
As
Object
<ScaffoldColumn(False)> _
Public ManageEquipmentDescription
As
Object
[MetadataType(typeof(ProductMD))]
public partial class Product {
public class ProductMD {
[ScaffoldColumn(false)]
public object QuantityPerUnit { get; set; }
// [ReadOnly(true)]
[UIHint("Text_RO")]
public object ProductName { get; set; }
}
}
Here is the VB annotations. Tested and it works. Note: We will probably support the ReadOnly attribute in the next version. It's not currently supported.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel
Imports System.Text.RegularExpressions
<MetadataType(GetType(ProductMD))> _
Partial Public Class Product
End Class
Public Class ProductMD
Private _QuantityPerUnit As Object
<ScaffoldColumn(False)> _
Public Property QuantityPerUnit() As Object
Get
Return _QuantityPerUnit
End Get
Set(ByVal value As Object)
_QuantityPerUnit = value
End Set
End Property
Private _ProductName As Object
' [ReadOnly(true)]
<UIHint("Text_RO")> _
Public Property ProductName() As Object
Get
Return _ProductName
End Get
Set(ByVal value As Object)
_ProductName = value
End Set
End Property
End Class
I just tested with EF and it works fine. You are probably missing the namespace so you have a naked partial class. That's why I added the reality check -
<ScaffoldColumn(False)>_
Public Property QuantityPerUnit() As Object
Get
Return _QuantityPerUnit
End Get
Set(ByVal value As Object)
_QuantityPerUnit = value
End Set
End Property
if you see the QuantityPerUnit column, your partial class is naked and not working.
>>I just tried the read-only suggestion and it does not work.
Correct, that will be a new feature. It's not working right now.
I think I figured out my problem. When I originally created the Text_RO.ascx user control, I only created "half". I needed to create both the text_RO.ascx AND Text_RO_Edit.ascx. I then had to make the Text_RO_Edit.ascx a literal just like the Text_RO.ascx.
The two user controls work together it appears.
Is this a correct assumption?
Hopefully you will see this post and be able to confirm this question for me and then mark this post as resolved? This was from last Friday.
Hi defyant_2004, I tried the ReadOnly attribute and it works perfect. I think you should place the ReadOnly attribute on top of the property in the auto generated data class, not in your own custom class. This is the easiest way to get things done, but the
only downside is we are modifying the auto gen class...
Sorry too quickly to jump to conclusion. Actually the ReadOnly atttribute doesn't seem to work correctly, at least I can't get it to work correctly. It does shows the column with just text string and without textbox in edit and insert, but if you try to
hit update it won't work, I guess when user hit the update link it tries to write to the ReadOnly field as well, despite user never change the value for that field (Because it's ReadOnly!). It still doen't work even after comment out the setter for that field
in the auto gen class.
Does anyone know how to get this to work? I am using 3.5 SP1, Linq to SQL
I know this is an old post but just want to update it with another solution in case it helps others. In place of the [ReadOnly(true)]
attribute, you can use the [Editable(false)]
attribute on the field you want to display but not allow edits on. If you annotate an Entity Reference (i.e. foreign key relationship) with the
ReadOnly
or Editable
attribute, the editable dropdown list on the Edit page gets replaced with a hyperlink.
Member
55 Points
337 Posts
Show Field But Do Not Allow Editing
Jan 23, 2009 02:50 PM|defyant_2004|LINK
Is it possible to make a field "un-editable" without having to hide it using Dynamic Data? I would like a field to be displayed to the user, but I don't want them to be able to edit it. Is there something I can do in the Custom Edit.aspx or Partial Class for that table?
Thanks for any advice.
Star
12522 Points
2214 Posts
Microsoft
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 03:12 PM|ricka6|LINK
One approach would be to copy DynamicData\FieldTemplates\Text.ascx to TextRO_Edit, then annotate you partial class with a UIHInt to use TextRO. On edit, you wouldn't get the edit text box, but the literal.
Stephen also has a tutorial on this. See read only fields DynamicData - Generate Columns/Rows (using IAutoFieldGenerator) - Part 5 listing 5 show a DynamicReadOnlyFiled.
All-Star
17915 Points
5680 Posts
MVP
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 04:16 PM|sjnaughton|LINK
See this post here noteditable attribute? and my answer there I'm going to blog that answer soon [:D]
oo! here it is now Making a Field Read-Only via the ReadOnlyAttribute – Dynamic Data I said I blog it soon [:D]
Dynamic Data ReadOnly Columns
Always seeking an elegant solution.
Contributor
3384 Points
1363 Posts
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 04:17 PM|davidebb|LINK
You can easily mark a field as non-editable (aka readonly) by using a readonly attribute on it (i.e. [ReadOnly(true)])
David
Member
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 04:24 PM|defyant_2004|LINK
I think your idea on creating a TextRO_Edit user control would do the trick. The field I want displayed - but not editable is not a primary key, it is just a regular field the user would like to see. They just don't want it to be edited.
Would you happen to know how to set the UIInt to use this TextRo user control in the partial class? I am using Dynamic Data Entity Framework with Visual Basic. My partial class for this table is shown below. Branch is the field I want to have displayed, but not editable. Thanks.
Imports
Microsoft.VisualBasicImports
System.Web.DynamicDataImports
System.ComponentModel.DataAnnotations Namespace TeamCapitalAssetsModel<MetadataType(
GetType(CapitalAssetsMetaData))> _ Partial Public Class CapitalAssets End Class Public Class CapitalAssetsMetaData <ScaffoldColumn(False)> _ Public CapitalAssetID As Object <ScaffoldColumn(False)> _ Public ManageEquipmentDescription As ObjectPublic Branch As Object
End
NamespaceStar
12522 Points
2214 Posts
Microsoft
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 04:39 PM|ricka6|LINK
I just tested this with NW and it works fine.
Here is the C# version (VB to follow)
Star
12522 Points
2214 Posts
Microsoft
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 04:54 PM|ricka6|LINK
Here is the VB annotations. Tested and it works. Note: We will probably support the ReadOnly attribute in the next version. It's not currently supported.
Member
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 05:07 PM|defyant_2004|LINK
I just tried the read-only suggestion and it does not work. I am trying the method above now and will post my results shortly. Thank you.
Imports
Microsoft.VisualBasicImports
System.Web.DynamicDataImports
System.ComponentModel.DataAnnotationsImports
System.ComponentModel Namespace TestModel<MetadataType(
GetType(ManagePermissionMetaData))> _ Partial Public Class ManagePermission End Class Public Class ManagePermissionMetaData<ScaffoldColumn(
False)> _ Public PermissionID As Object <[ReadOnly](True)> _ Public Branch As Object End ClassEnd
NamespaceMember
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 05:15 PM|defyant_2004|LINK
I am still allowed to update the data for this field. I am using Dynamic Data Entity Framework,, is that why it may not work?
Star
12522 Points
2214 Posts
Microsoft
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 05:30 PM|ricka6|LINK
I just tested with EF and it works fine. You are probably missing the namespace so you have a naked partial class. That's why I added the reality check -
if you see the QuantityPerUnit column, your partial class is naked and not working.
>>I just tried the read-only suggestion and it does not work.
Correct, that will be a new feature. It's not working right now.
Member
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 26, 2009 02:28 PM|defyant_2004|LINK
Ricka6,
I think I figured out my problem. When I originally created the Text_RO.ascx user control, I only created "half". I needed to create both the text_RO.ascx AND Text_RO_Edit.ascx. I then had to make the Text_RO_Edit.ascx a literal just like the Text_RO.ascx. The two user controls work together it appears.
Is this a correct assumption?
Hopefully you will see this post and be able to confirm this question for me and then mark this post as resolved? This was from last Friday.
Thanks for help.
Star
12522 Points
2214 Posts
Microsoft
Re: Show Field But Do Not Allow Editing
Jan 26, 2009 02:44 PM|ricka6|LINK
Yes, that's why I originally suggested One approach would be to copy DynamicData\FieldTemplates\Text.ascx to TextRO_Edit
As when you copy the control, the code behind file is automatically copied too.
Member
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 26, 2009 03:11 PM|defyant_2004|LINK
Yes, it was the fact that I did not include _edit.
Thanks again for your help.
Member
15 Points
15 Posts
Re: Show Field But Do Not Allow Editing
Jan 15, 2010 07:59 PM|zhili|LINK
Hi defyant_2004, I tried the ReadOnly attribute and it works perfect. I think you should place the ReadOnly attribute on top of the property in the auto generated data class, not in your own custom class. This is the easiest way to get things done, but the only downside is we are modifying the auto gen class...
Member
15 Points
15 Posts
Re: Show Field But Do Not Allow Editing
Jan 15, 2010 10:27 PM|zhili|LINK
Sorry too quickly to jump to conclusion. Actually the ReadOnly atttribute doesn't seem to work correctly, at least I can't get it to work correctly. It does shows the column with just text string and without textbox in edit and insert, but if you try to hit update it won't work, I guess when user hit the update link it tries to write to the ReadOnly field as well, despite user never change the value for that field (Because it's ReadOnly!). It still doen't work even after comment out the setter for that field in the auto gen class.
Does anyone know how to get this to work? I am using 3.5 SP1, Linq to SQL
All-Star
17915 Points
5680 Posts
MVP
Re: Show Field But Do Not Allow Editing
Jan 16, 2010 04:45 AM|sjnaughton|LINK
Hi Zhili, have a look at these articles here : Making a Field Read-Only via the ReadOnlyAttribute – Dynamic Data and Making Individual Tables Read Only – Dynamic Data
Dynamic Data ReadOnly Attribute
Always seeking an elegant solution.
None
0 Points
2 Posts
Re: Show Field But Do Not Allow Editing
Jun 03, 2013 09:01 PM|san2|LINK
I know this is an old post but just want to update it with another solution in case it helps others. In place of the [ReadOnly(true)] attribute, you can use the [Editable(false)] attribute on the field you want to display but not allow edits on. If you annotate an Entity Reference (i.e. foreign key relationship) with the ReadOnly or Editable attribute, the editable dropdown list on the Edit page gets replaced with a hyperlink.