none
FormField.ControlMode on postback doesnt change? RRS feed

  • Question

  • ok I am confused as to why on postback the ControlMode of FormField on my custom form wouldnt change. To simplify here's how my code looks:

    <SharePoint:FormField runat="server" id="ff1" FieldName="Description1" ControlMode="Edit"/>

    protected override OnLoad()
    {

     if (!IsPostback)
     {
       ff1.ControlMode = SPControlMode.Display;
    //this works fine and shows the control is Display mode
     }

     ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);

    }


    protected void (ddl_SelectedIndexChanged, EventArgs e)
    {
      ff1.ControlMode = SPControlMode.Display;
    //the debugger hits this, shows the ControlMode changed to Display  but the page still shows ff1 in Edit mode
    }


    can anyone tell me what I am doing wrong? I need the ability to change controlmode anytime per requirements.

    Thursday, April 15, 2010 8:03 PM

Answers

  • The SelectedIndexChanged event handler may occur too late for the ControlMode property to be changed.

    The FormField's nested BaseFieldControl is created in its CreateChildControls method override which is probably invoked during or immediately after the OnLoad event.

    If this is so then the FormField is not designed for ControlMode changing in post-backs.

    A possible and ugly work-around would be to test for post-back triggered by the drop down list in the OnLoad (or possibly the OnInit) event the hard way: 1st checking if Page.IsPostBack is true, and then inspecting the Request.Form["__EVENTTARGET"] for the drop down control's name ( which should be: ddl.ClientID.Replace("_", "$") ) and then getting the actually selected index - Request.Form["ddl's name"]. The changing of the ControlMode property then will be possible in the OnLoad event.

    • Marked as answer by Chengyi Wu Monday, April 19, 2010 1:59 AM
    Thursday, April 15, 2010 9:16 PM

All replies

  • If I change FormField to NoteField for example, it works as expected. hmmmm, it defeats the advantage of using FormField then unless the form doesnt do postbacks, which I assume would hardly be the case. I will post an update after I make sure it works for other FormFields i have.
    Thursday, April 15, 2010 9:14 PM
  • The SelectedIndexChanged event handler may occur too late for the ControlMode property to be changed.

    The FormField's nested BaseFieldControl is created in its CreateChildControls method override which is probably invoked during or immediately after the OnLoad event.

    If this is so then the FormField is not designed for ControlMode changing in post-backs.

    A possible and ugly work-around would be to test for post-back triggered by the drop down list in the OnLoad (or possibly the OnInit) event the hard way: 1st checking if Page.IsPostBack is true, and then inspecting the Request.Form["__EVENTTARGET"] for the drop down control's name ( which should be: ddl.ClientID.Replace("_", "$") ) and then getting the actually selected index - Request.Form["ddl's name"]. The changing of the ControlMode property then will be possible in the OnLoad event.

    • Marked as answer by Chengyi Wu Monday, April 19, 2010 1:59 AM
    Thursday, April 15, 2010 9:16 PM
  • Thanks Stefan, yea thats the only thing i can think of too. The workaround isnt worth the effort, i would rather change my fields to particular type.
    Thursday, April 15, 2010 9:23 PM