Hi M@roc, yes this is doable but not out of the box.
What you need to do is:
<div mce_keep="true">Use the ReadOnlyAttribute in System.ComponentModel and attribute upo the column you want as read only in edit mode</div>
<div mce_keep="true">Create an class that implements IAutoFieldGenrator.</div>
<div mce_keep="true">A custom DynamicField.</div>
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);
}
}
}
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?
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]
Dynamic DataReadOnlyFields
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
m@rco
Member
16 Points
44 Posts
noteditable attribute?
Jan 05, 2009 09:34 AM|LINK
I want to annotate my column that it is not editable in edit mode...
is this possible?
sjnaughton
All-Star
27320 Points
5459 Posts
MVP
Re: noteditable attribute?
Jan 05, 2009 01:03 PM|LINK
Hi M@roc, yes this is doable but not out of the box.
What you need to do is:
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:
In the Page_Init add the BOLD ITALIC lines
Dynamic Data Implement Read Only Fields
Always seeking an elegant solution.
m@rco
Member
16 Points
44 Posts
Re: noteditable attribute?
Jan 05, 2009 02:06 PM|LINK
I think this might work!
thanx a lot Steve!
d@vid7
Member
4 Points
2 Posts
Re: noteditable attribute?
Mar 26, 2009 02:15 PM|LINK
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?
sjnaughton
All-Star
27320 Points
5459 Posts
MVP
Re: noteditable attribute?
Mar 26, 2009 02:29 PM|LINK
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]
Dynamic Data ReadOnly Fields
Always seeking an elegant solution.
d@vid7
Member
4 Points
2 Posts
Re: noteditable attribute?
Mar 26, 2009 02:45 PM|LINK
m@rco had already hit the "answer" button. (I did check.)
5 a.m.? ... and I thought *I* was a night owl ... :-)
Joe Gill
Member
14 Points
3 Posts
Re: noteditable attribute?
Mar 30, 2009 11:38 AM|LINK
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
Joericka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: noteditable attribute?
Mar 30, 2009 07:10 PM|LINK
Good Idea. Keep in mind with ASP.NET Dynamic Data 4.0 Preview 3 you can use the ReadOnly attribute.