I'm getting the following error:

The Controls collection cannot be modified because the control contains code blocks

I just want to show/hide controls based on page values. How do I do that?

My current code:

<asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">|
<SharePointWebControls:LookupField runat="server" id="lfITAlertType" FieldName="ITAlertType"/>
  if (SPContext.Current.ListItem["ITAlertType"] == "BOB") {

I switched to this approach - placing the code in a script tag in the header:

    <%@ Import Namespace="Microsoft.SharePoint" %>
    <asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
    <SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
    <asp:Content ContentPlaceholderID="PlaceHolderAdditionalPageHead" runat="server">
        <SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
        <script runat="server" type="text/c#">
            public void Page_Load()
                var itAlertType = SPContext.Current.ListItem["ITAlertType"];        
                string sITAlertType = new SPFieldLookupValue(itAlertType.ToString()).LookupValue;
                lblTest.Text = sITAlertType;
    <asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">
        <PublishingWebControls:EditModePanel ID="EditModePanel2" runat="server" CssClass="edit-mode-panel title-edit">
            <SharePointWebControls:LookupField runat="server" id="ddcfTest" FieldName="ITAlertType" />
        <asp:Label runat="server" id="lblTest" text="BOB" />
  • I've updated this to show that you should use PlaceHolderAdditionalPageHead for the code block otherwise you might get issues stuffing stuff into the Title tag.
    – PeterX
    Oct 28 '15 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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