G
gerry
I am trying to set the readonly status of fields in a DetailsView control
dynamically at runtime.
I have attempted this using the ItemCreated event ( see bottom ).
This works fine for BoundFields , but there is no ReadOnly property for
TemplateFields.
For template fields i tried getting the control to use the ItemTemplate as
opposed to the EditTemplate - with no success.
So my question is - how do we dynamically select DetailsView the field
templates at runtime ?
Gerry
protected void DetailsView1_ItemCreated( object sender , EventArgs e )
{
if ( !RestrictFields )
return;
DetailsView dv = sender as DetailsView;
foreach (DataControlField field in dv.Fields)
{
switch(field.SortExpression)
{
// fields that should always be updateable
case "Field1":
case "Field2":
break;
default:
if ( field is BoundField )
{
( (BoundField) field ).ReadOnly = true; ;
}
else if ( field is TemplateField )
{
TemplateField tfield = field as TemplateField;
switch(1){
case 1:
// this causes control to recreate this field
resulting endless loop into here
if ( tfield.EditItemTemplate != null)
tfield.EditItemTemplate = null;
break;
case 2:
// this causes viewstate error on postback
if (
tfield.ItemTemplate!=tfield.EditItemTemplate)
tfield.EditItemTemplate =
tfield.ItemTemplate;
break;
}
}
break;
}
}
}
dynamically at runtime.
I have attempted this using the ItemCreated event ( see bottom ).
This works fine for BoundFields , but there is no ReadOnly property for
TemplateFields.
For template fields i tried getting the control to use the ItemTemplate as
opposed to the EditTemplate - with no success.
So my question is - how do we dynamically select DetailsView the field
templates at runtime ?
Gerry
protected void DetailsView1_ItemCreated( object sender , EventArgs e )
{
if ( !RestrictFields )
return;
DetailsView dv = sender as DetailsView;
foreach (DataControlField field in dv.Fields)
{
switch(field.SortExpression)
{
// fields that should always be updateable
case "Field1":
case "Field2":
break;
default:
if ( field is BoundField )
{
( (BoundField) field ).ReadOnly = true; ;
}
else if ( field is TemplateField )
{
TemplateField tfield = field as TemplateField;
switch(1){
case 1:
// this causes control to recreate this field
resulting endless loop into here
if ( tfield.EditItemTemplate != null)
tfield.EditItemTemplate = null;
break;
case 2:
// this causes viewstate error on postback
if (
tfield.ItemTemplate!=tfield.EditItemTemplate)
tfield.EditItemTemplate =
tfield.ItemTemplate;
break;
}
}
break;
}
}
}