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.
defyant_2004
Member
55 Points
337 Posts
Show Field But Do Not Allow Editing
Jan 23, 2009 06:50 PM|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.
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 07:12 PM|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.
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 08:16 PM|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.
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 08:17 PM|LINK
You can easily mark a field as non-editable (aka readonly) by using a readonly attribute on it (i.e. [ReadOnly(true)])
David
defyant_2004
Member
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 08:24 PM|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 ClassEnd
Namespacericka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 08:39 PM|LINK
I just tested this with NW and it works fine.
Here is the C# version (VB to follow)
[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; } } }ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 08:54 PM|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.
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 Classdefyant_2004
Member
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 09:07 PM|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
Namespacedefyant_2004
Member
55 Points
337 Posts
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 09:15 PM|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?
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: Show Field But Do Not Allow Editing
Jan 23, 2009 09:30 PM|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 -
<ScaffoldColumn(False)>_ Public Property QuantityPerUnit() As Object Get Return _QuantityPerUnit End Get Set(ByVal value As Object) _QuantityPerUnit = value End Set End Propertyif 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.