2

I am attempting to have a field that is set to ControlMode="Edit" by default be conditional to be set to ControlMode="Display" based on the value that is set to default above it.

For example: [Field1] is populated by a previous process to either "Yes" or "No". For [Field2] I would like to to be ControlMode="Edit" if [Field1] is set to "Yes" and ControlMode="Display" if [Field1] is set to "No". I have tried the following:

<script type="text/javascript"> 
if ("ff1"="Yes") 
{ <td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff73{$Pos}" ControlMode="Display" FieldName="EMail_x0020_Address" __designer:bind="{ddwrt:DataBind('u',concat('ff73',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@EMail_x0020_Address')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff73description{$Pos}" FieldName="EMail_x0020_Address" ControlMode="Display"/>
                    </td>
} 
ELSE 
{<td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff73{$Pos}" ControlMode="Edit" FieldName="EMail_x0020_Address" __designer:bind="{ddwrt:DataBind('u',concat('ff73',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@EMail_x0020_Address')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff73description{$Pos}" FieldName="EMail_x0020_Address" ControlMode="Edit"/>
                    </td>
}

I have placed this within the table.

Could anyone help me understand what I am doing wrong and how I can overcome this issue?

0

2 Answers 2

3

There is a cleaner, pure XSLT way, to achieve what you are trying to do; no need to use javascript. By the way, always remember that XSLT is evaluated and executed first, way before your javascript gets called.

<xsl:variable name="ControlMode">
    <xsl:choose>
        <xsl:when test="@Field1 = 'Yes'">
             Edit
        </xsl:when>
        <xsl:otherwise>
            Display
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<SharePoint:FormField runat="server" id="ff73{$Pos}" ControlMode="{$ControlMode}" FieldName="EMail_x0020_Address" __designer:bind="{ddwrt:DataBind('u',concat('ff73',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@EMail_x0020_Address')}"/>

UPDATE: how to conditionally highlight a table cell, based on Field1 value

<td>
<xsl:if test="@Field1 = 'Yes'">
     <xsl:attribute name="style">background-color:yellow;</xsl:attribute>
</xsl:if>
//this is just for demonstration purpose, it would be better to use a css class, instead of inline style
11
  • Thank you MdMazzotti. Where, when editing in Advanced Mode, would this code be placed? Apr 21, 2015 at 17:19
  • Yep, I thought you were already there (your custom edit form > Edit file in advanced Mode). Where did you put the code you showed otherwise?
    – MdMazzotti
    Apr 21, 2015 at 17:26
  • Sorry, I believe I found where to place it. I placed the code in the <TD> for the field entry. My next question is in your code you reference test="@Field1 = 'Yes'"> what label would I replace @Field1 with for the field I need to reference? Apr 21, 2015 at 20:00
  • 1
    You must use the Static Name, like this: @YourStaticName (the @ is required, as it is part of the xslt syntax)
    – MdMazzotti
    Apr 21, 2015 at 20:24
  • 1
    @CalebFortner I saw your comment, then updated my answer
    – MdMazzotti
    Apr 21, 2015 at 22:12
0

Looks like your script runs once and is not looking for active changes in the form. I used the the jQuery change()function to look for a change in a drop down field (Should not be hard to change to check box). I added the script to the form using a code snippet web part. When 'translation' is selected it exposes a hidden field to allow a selection of languages. If it is not it hides the extra field. I got the name of the field (ProjectType_f0f12dc5-0065-49b0-8316-21244af42410_$RadioButtonChoiceField) by using SharePoint Designer to look at the form and get the name. Code snippet below:

<script type="text/javascript">
     $(document).ready(function()
      {
          $("h3.ms-standardheader:contains('Requested Language')").closest("tr").hide();

          $('input:radio[name="ProjectType_f0f12dc5-0065-49b0-8316-21244af42410_$RadioButtonChoiceField"]').change(
        function(){

            // checks that the clicked radio button is the one of value 'Yes'
            if ($(this).val() == 'Translation') {

                // If button checked, shows Translation section
                $("h3.ms-standardheader:contains('Requested Language')").closest("tr").show();
            }
            else {

                // if button is not checked the Translation section is hidden
                $("h3.ms-standardheader:contains('Requested Language')").closest("tr").hide();
            }
        });
     });
    </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.