Answered by:
noteditable attribute?

Question
-
User-1735691636 posted
I want to annotate my column that it is not editable in edit mode...
is this possible?
Monday, January 5, 2009 5:34 AM
Answers
-
User-330204900 posted
Hi M@roc, yes this is doable but not out of the box.
What you need to do is:
-
Use the ReadOnlyAttribute in System.ComponentModel and attribute upo the column you want as read only in edit mode
-
Create an class that implements IAutoFieldGenrator.
-
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<DYNAMICFIELD>(); 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<READONLYATTRIBUTE>().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
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 5, 2009 9:03 AM -
-
User-82275785 posted
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")]<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>public object UpdateDate;
HTH
Joe<o:p></o:p>- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 30, 2009 7:38 AM
All replies
-
User-330204900 posted
Hi M@roc, yes this is doable but not out of the box.
What you need to do is:
-
Use the ReadOnlyAttribute in System.ComponentModel and attribute upo the column you want as read only in edit mode
-
Create an class that implements IAutoFieldGenrator.
-
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<DYNAMICFIELD>(); 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<READONLYATTRIBUTE>().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
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 5, 2009 9:03 AM -
-
User-1735691636 posted
I think this might work!
thanx a lot Steve!
Monday, January 5, 2009 10:06 AM -
User750121968 posted
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?
Thursday, March 26, 2009 10:15 AM -
User-330204900 posted
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. [:(]
And of ryou info Rick says it will be in there in the ASP.Net 4.0 [:D]
Thursday, March 26, 2009 10:29 AM -
User750121968 posted
m@rco had already hit the "answer" button. (I did check.)
5 a.m.? ... and I thought *I* was a night owl ... :-)
Thursday, March 26, 2009 10:45 AM -
User-82275785 posted
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")]<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>public object UpdateDate;
HTH
Joe<o:p></o:p>- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 30, 2009 7:38 AM -
User-1005219520 posted
Good Idea. Keep in mind with ASP.NET Dynamic Data 4.0 Preview 3 you can use the ReadOnly attribute.
Monday, March 30, 2009 3:10 PM