noteditable attribute?

Last post 03-30-2009 3:10 PM by ricka6. 7 replies.

Sort Posts:

  • noteditable attribute?

    01-05-2009, 5:34 AM
    • Member
      13 point Member
    • m@rco
    • Member since 11-25-2008, 1:29 PM
    • Posts 36

    I want to annotate my column that it is not editable in edit mode...

    is this possible?

  • Re: noteditable attribute?

    01-05-2009, 9:03 AM
    Answer
    • Star
      12,384 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,575
    • TrustedFriends-MVPs

    Hi M@roc, yes this is doable but not out of the box.

    What you need to do is:

    1. Use the ReadOnlyAttribute in System.ComponentModel and attribute upo the column you want as read only in edit mode
    2. Create an class that implements IAutoFieldGenrator.
    3. A custom DynamicField.

    And here's the sample: 

    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Web.DynamicData;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public class FieldsManager : IAutoFieldGenerator
    {
        protected MetaTable _table;
    
        public FieldsManager(MetaTable table)
        {
            _table = table;
        }
    
        public ICollection GenerateFields(Control control)
        {
            var oFields = new List();
    
            foreach (var column in _table.Columns)
            {
                // carry on the loop at the next column  
                // if scaffold table is set to false or DenyRead
                if (!column.Scaffold || column.IsLongString)
                    continue;
    
                DynamicField f;
    
                // here we check to dee
                if (column.Attributes.OfType().DefaultIfEmpty(new ReadOnlyAttribute(false)).FirstOrDefault().IsReadOnly)
                    f = new DynamicReadonlyField();
                else
                    f = new DynamicField();
                    
                f.DataField = column.Name;
                oFields.Add(f);
            }
            return oFields;
        }
    }
    
    // special thanks to david Ebbo for this
    public class DynamicReadonlyField : DynamicField
    {
        public override void InitializeCell(
            DataControlFieldCell cell,
            DataControlCellType cellType,
            DataControlRowState rowState,
            int rowIndex)
        {
            if (cellType == DataControlCellType.DataCell)
            {
                var control = new DynamicControl() { DataField = DataField };
    
                // Copy various properties into the control
                control.UIHint = UIHint;
                control.HtmlEncode = HtmlEncode;
                control.NullDisplayText = NullDisplayText;
    
                // this the default for DynamicControl and has to be 
                // manually changed you do not need this line of code
                // its there just to remind us what we are doing.
                control.Mode = DataBoundControlMode.ReadOnly;
    
                cell.Controls.Add(control);
            }
            else
            {
                base.InitializeCell(cell, cellType, rowState, rowIndex);
            }
        }
    }

    And in the Edit page add: 

    protected void Page_Init(object sender, EventArgs e)
    {
        DynamicDataManager1.RegisterControl(DetailsView1);
        table = DetailsDataSource.GetTable();
        DetailsView1.RowsGenerator = new FieldsManager(table);
    }
    

    In the Page_Init add the BOLD ITALIC lines 

     

    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: noteditable attribute?

    01-05-2009, 10:06 AM
    • Member
      13 point Member
    • m@rco
    • Member since 11-25-2008, 1:29 PM
    • Posts 36

    I think this might work!

    thanx a lot Steve!

  • Re: noteditable attribute?

    03-26-2009, 10:15 AM
    • Member
      4 point Member
    • d@vid7
    • Member since 03-25-2009, 9:50 PM
    • Posts 2

    Thanks, Steve.  This is exactly what I was looking for.  (Although I still find it hard to believe this functionality isn't "in the box".)

    FYI, it looks like some stuff got stripped out of your code snippet.  For example, what's online is
        // Dim oFields As New List
        var oFields = new List();
    which I believe should read
        // Dim oFields As New List(Of DynamicField)
        var oFields = new List<DynamicField>

    I'm guessing this has something to do with the presence of the greater-than and less-than symbols in the C# code?

  • Re: noteditable attribute?

    03-26-2009, 10:29 AM
    • Star
      12,384 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,575
    • TrustedFriends-MVPs

    Yep that always happens and I don't always remember to correct it sorry. We have alerted the forum, but I believe it is a long standing issue ever since Generics was introduced. Sad

    And of ryou info Rick says it will be in there in the ASP.Net 4.0 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: noteditable attribute?

    03-26-2009, 10:45 AM
    • Member
      4 point Member
    • d@vid7
    • Member since 03-25-2009, 9:50 PM
    • Posts 2

    m@rco had already hit the "answer" button.  (I did check.)

    5 a.m.?  ... and I thought *I* was a night owl ... :-)

  • Re: noteditable attribute?

    03-30-2009, 7:38 AM
    Answer
    • Member
      14 point Member
    • Joe Gill
    • Member since 03-30-2009, 9:54 AM
    • Posts 3

    Guys,

    I went for a more simple solutions as follows

    Make two copies of the Text.ascx field template and name one ReadOnly.ascx and the other ReadOnly_Edit.ascx then annotate the field as follows

    [UIHint("ReadOnly")]

                public object UpdateDate;

    HTH

    Joe

     

  • Re: noteditable attribute?

    03-30-2009, 3:10 PM
    • Contributor
      5,970 point Contributor
    • ricka6
    • Member since 06-25-2008, 6:04 PM
    • Redmond
    • Posts 956
    • AspNetTeam
      Moderator

    Good Idea. Keep in mind with ASP.NET Dynamic Data 4.0 Preview 3  you can use the ReadOnly attribute.

    Rick -ASP.Net UE MVC FAQ   Rick on MVC & Dynamic Data   
Page 1 of 1 (8 items)