Selectively set readonly for detailsview fields

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;
}
}
}
 
W

Walter Wang [MSFT]

Hi Gerry,

You can use the ModeChanged event:


protected void DetailsView1_ModeChanged(object sender, EventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
foreach (DataControlField dcf in DetailsView1.Fields)
{
TemplateField tf = dcf as TemplateField;
if (tf != null)
{
if (tf.EditItemTemplate != null)
{
tf.EditItemTemplate = null;
}
}
}
}
}

Please feel free to let me know if there's anything unclear. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

gerry

Thanks Walter

But this still generates a viewstate error on postback.

I did resolve the problem by placing this code in the DetailsView.Init
method.

Gerry
 
W

Walter Wang [MSFT]

Hi Gerry,

Oops, I'm sorry that I only tested the recursive issue but not the actual
saving function. Thanks for sharing your solution here.

Please feel free to let me know if there's anything else I can help.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top