Selectively set readonly for detailsview fields

Discussion in 'ASP .Net' started by gerry, Sep 24, 2007.

  1. gerry

    gerry Guest

    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;
    }
    }
    }
    gerry, Sep 24, 2007
    #1
    1. Advertising

  2. 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 (, 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.
    Walter Wang [MSFT], Sep 25, 2007
    #2
    1. Advertising

  3. gerry

    gerry Guest

    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



    ""Walter Wang [MSFT]"" <> wrote in message
    news:VmNyzWy$...
    > 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 (, 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, Sep 25, 2007
    #3
  4. 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 (, 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.
    Walter Wang [MSFT], Sep 26, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    3,008
    Flinky Wisty Pomm
    Apr 2, 2006
  2. =?Utf-8?B?TWlrZSBE?=

    Dynamic ReadOnly in DetailsView

    =?Utf-8?B?TWlrZSBE?=, Oct 29, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    545
  3. Jonathan Hyatt
    Replies:
    1
    Views:
    1,903
    Jonathan Hyatt
    Jun 8, 2004
  4. mt
    Replies:
    1
    Views:
    305
    Phillip Williams
    Apr 10, 2006
  5. cotton_gear

    How to reset form fields selectively ?

    cotton_gear, Nov 18, 2005, in forum: Javascript
    Replies:
    4
    Views:
    129
    Thomas 'PointedEars' Lahn
    Nov 19, 2005
Loading...

Share This Page