Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Selectively set readonly for detailsview fields

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gerry - 24 Sep 2007 17:27 GMT
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;
       }
   }
}
Walter Wang [MSFT] - 25 Sep 2007 04:39 GMT
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 (wawang@online.microsoft.com, 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.
gerry - 25 Sep 2007 14:38 GMT
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

> Hi Gerry,
>
[quoted text clipped - 31 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Walter Wang [MSFT] - 26 Sep 2007 07:21 GMT
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 (wawang@online.microsoft.com, 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.