How to access compositefields in code behind file when we have rendered list items with compositefields in custom template

Unanswered How to access compositefields in code behind file when we have rendered list items with compositefields in custom template

  • 4 aprilie 2012 09:55
     
      Are cod

    I'm making modifications to an existing sharepoint project. There are custom SharePoint RenderingTemplates for Display and Edit forms. I have checked that the custom template names for Display and Edit form is added to content type.
    In this case Edit From is called 'myEditForm'. In myEditform there is a compositeField with id 'CompositeField21' which is a choice(dropdown) field.
    I have to add a feature Where I select a specific value from the list and after I have selected a value, compositeField should become visible (CompositeField18) which is too a choice field.
    The problem is that I'm not able to access any compositefields in my code behind file. It always returns a null value.
    What should I do to make compositefield visible from code behind file when I have selected a value from a choice field?
    On EditForm.aspx I have inherited from myListPage class:

    <%@ Page Language="C#" MasterPageFile="~site/_catalogs/masterpage/myMasterPage.master"
        Inherits="namespace.myListPage,projectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx" %>
    
    This is my customTemplate.ascx file:
    
    <%@ Control Language="C#" AutoEventWireup="false" %>
    <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx"
        Namespace="Microsoft.SharePoint.WebControls" %>
    <%@ Register TagPrefix="SPHttpUtility" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx"
        Namespace="Microsoft.SharePoint.Utilities" %>
    <%@ Register TagPrefix="wssuc" TagName="ToolBar" Src="~/_controltemplates/ToolBar.ascx" %>
    <%@ Register TagPrefix="wssuc" TagName="ToolBarButton" Src="~/_controltemplates/ToolBarButton.ascx" %>
    
    <SharePoint:RenderingTemplate ID="myEditForm" runat="server">
        <Template>
            <span id='part1'>
                <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator="&nbsp;"
                    runat="server">
                    <template_rightbuttons>
    			<SharePoint:NextPageButton ID="NextPageButton1" runat="server"/>
    			<SharePoint:SaveButton ID="SaveButton1" runat="server"/>
    			<SharePoint:GoBackButton ID="GoBackButton1" runat="server"/>
    		</template_rightbuttons>
                </wssuc:ToolBar>
                <div id="TenderDiv1" class="TenderDiv">
                    <table class="ms-formtable" style="margin-" border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
    			<!-- This is the choice field -->
                            <SharePoint:CompositeField runat="server" ControlMode="Edit" FieldName="example_field1"
                                ID="CompositeField21" TemplateName="myListDisplayCompositeField" />
                        </tr>
                        <tr>
    			<!-- This field we want to become visible when we have selected a specific value from choice field -->
                            <SharePoint:CompositeField runat="server" ControlMode="Edit" Visible="false" FieldName="example_field2"
                           	    ID="CompositeField18" TemplateName="myListDisplayCompositeField" />
                        </tr>
    		</table>
    	    </div>
    	</span>
        </Template>
    </SharePoint:RenderingTemplate>
    <!-- I haven't touched to this -->
    <SharePoint:RenderingTemplate ID="myListDisplayCompositeField" runat="server">
        <Template>
            <td nowrap="true" valign="top" width="155px" class="ms-formlabel">
                <span class="ms-standardheader">
                    <SharePoint:FieldLabel ID="FieldLabel1" runat="server" />
                </span>
            </td>
            <td valign="top" class="ms-formbody" width="350px" id="SPField<SharePoint:FieldProperty PropertyName='Type' runat='server'/>">
                <SharePoint:FormField ID="FormField4" runat="server" />
                <SharePoint:AppendOnlyHistory ID="AppendOnlyHistory1" runat="server" />
            </td>
        </Template>
    </SharePoint:RenderingTemplate>
    

    This is my code behind file:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.WebControls;
    
    namespace myNamespace
    {
        public class myListPage : Microsoft.SharePoint.WebPartPages.WebPartPage
        {
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
    
                //Check if we are in Edit-mode
                if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
                {
                    SPWeb web = SPContext.Current.Web;
                    SPList list = web.Lists["myList"];
                    SPFieldChoice dropDown = (SPFieldChoice)list.Fields["myListField"];
                    string SelectedValue = dropDown.GetFieldValueAsText("myValue");
    
                    if (SelectedValue == "myValue")
                    {
    	
    	            // get the CompositeField with ID 'CompositeField18' but I get a null value
                        CompositeField myHiddenCompositeField = (CompositeField)FindControl("CompositeField18");
                        if (myHiddenCompositeField != null)
                        {
    	                // This should make the field visible
                            myHiddenCompositeField.Visible = true;
                        }
                        else
                        {
                            Response.Write("Control not found");
                        }
                    }
                }
            }
        }
    }

Toate mesajele

  • 3 mai 2012 12:44
     
     
    I got this working with jQuery. I entered scripts to .ascx page to make this work. Case closed from my side.