locked
Show Field But Do Not Allow Editing RRS feed

  • Question

  • User-266149547 posted

    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.

    Friday, January 23, 2009 2:50 PM

Answers

  • User-1005219520 posted
    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.

    Stephen also has a tutorial on this. See read only fields DynamicData - Generate Columns/Rows (using IAutoFieldGenerator) - Part 5 listing 5 show a DynamicReadOnlyFiled.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 3:12 PM
  • User-330204900 posted

    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]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:16 PM
  • User-1005219520 posted

     

    I just tested this with NW and it works fine.

    1. Copy Text.asc to Text_RO.asc
    2. Annotate your partial class entity with UIHint.

    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; }
            }
        }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:39 PM
  • User-1005219520 posted

    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
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:54 PM
  • User-1005219520 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 5:30 PM
  • User-1005219520 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2009 2:44 PM

All replies

  • User-1005219520 posted
    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.

    Stephen also has a tutorial on this. See read only fields DynamicData - Generate Columns/Rows (using IAutoFieldGenerator) - Part 5 listing 5 show a DynamicReadOnlyFiled.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 3:12 PM
  • User-330204900 posted

    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]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:16 PM
  • User1641955678 posted

    You can easily mark a field as non-editable (aka readonly) by using a readonly attribute on it (i.e. [ReadOnly(true)])

    David

    Friday, January 23, 2009 4:17 PM
  • User-266149547 posted

    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.VisualBasic

    Imports System.Web.DynamicData

    Imports 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 Object

     

    Public Branch As Object 

     

    End Class

    End Namespace

    Friday, January 23, 2009 4:24 PM
  • User-1005219520 posted

     

    I just tested this with NW and it works fine.

    1. Copy Text.asc to Text_RO.asc
    2. Annotate your partial class entity with UIHint.

    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; }
            }
        }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:39 PM
  • User-1005219520 posted

    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
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 4:54 PM
  • User-266149547 posted

    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.VisualBasic

    Imports System.Web.DynamicData

    Imports System.ComponentModel.DataAnnotations

    Imports 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 Class

    End Namespace

    Friday, January 23, 2009 5:07 PM
  • User-266149547 posted

    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?

    Friday, January 23, 2009 5:15 PM
  • User-1005219520 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 23, 2009 5:30 PM
  • User-266149547 posted

    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.

    Monday, January 26, 2009 2:28 PM
  • User-1005219520 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2009 2:44 PM
  • User-266149547 posted

    Yes, it was the fact that I did not include _edit.

    Thanks again for your help.

    Monday, January 26, 2009 3:11 PM
  • User1560082622 posted

    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.VisualBasic

    Imports System.Web.DynamicData

    Imports System.ComponentModel.DataAnnotations

    Imports 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 Class

    End Namespace

    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...

    Friday, January 15, 2010 7:59 PM
  • User1560082622 posted

    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


    Friday, January 15, 2010 10:27 PM
  • Saturday, January 16, 2010 4:45 AM
  • User759379893 posted

    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.

    Monday, June 3, 2013 9:01 PM