Show Field But Do Not Allow Editing

Last post 01-26-2009 3:11 PM by defyant_2004. 12 replies.

Sort Posts:

  • Show Field But Do Not Allow Editing

    01-23-2009, 2:50 PM

    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.

  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 3:12 PM
    Answer
    • Contributor
      5,362 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 892
    • AspNetTeam
      Moderator
    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.

    Rick -ASP.Net UE  Rick on MVC & Dynamic Data   
  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 4:16 PM
    Answer
    • Star
      11,463 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,418
    • TrustedFriends-MVPs

    See this post here noteditable attribute? and my answer there I'm going to blog that answer soon Big Smile

    oo! here it is now Making a Field Read-Only via the ReadOnlyAttribute – Dynamic Data I said I blog it soon Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 4:17 PM
    • Contributor
      5,655 point Contributor
    • davidebb
    • Member since 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 1,123
    • AspNetTeam

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

    David

  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 4:24 PM

    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

  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 4:39 PM
    Answer
    • Contributor
      5,362 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 892
    • AspNetTeam
      Moderator

     

    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; }
            }
        }
     
    Rick -ASP.Net UE  Rick on MVC & Dynamic Data   
  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 4:54 PM
    Answer
    • Contributor
      5,362 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 892
    • AspNetTeam
      Moderator

    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
     
    Rick -ASP.Net UE  Rick on MVC & Dynamic Data   
  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 5:07 PM

    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

  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 5:15 PM

    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?

  • Re: Show Field But Do Not Allow Editing

    01-23-2009, 5:30 PM
    Answer
    • Contributor
      5,362 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 892
    • AspNetTeam
      Moderator

    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.

    Rick -ASP.Net UE  Rick on MVC & Dynamic Data   
  • Re: Show Field But Do Not Allow Editing

    01-26-2009, 2:28 PM

    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.

  • Re: Show Field But Do Not Allow Editing

    01-26-2009, 2:44 PM
    Answer
    • Contributor
      5,362 point Contributor
    • ricka6
    • Member since 06-25-2008, 10:04 PM
    • Redmond
    • Posts 892
    • AspNetTeam
      Moderator

    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.

    Rick -ASP.Net UE  Rick on MVC & Dynamic Data   
  • Re: Show Field But Do Not Allow Editing

    01-26-2009, 3:11 PM

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

    Thanks again for your help.

Page 1 of 1 (13 items)