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();
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

Always seeking an elegant solution.
[Oh! If olny I colud tpye!]
c# Bits blogOh, and don't forget to mark as answer any posts that help you
