+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to access masterpage server controls from MVC controller in hybrid mode?

How to access masterpage server controls from MVC controller in hybrid mode?

7 posts, 0 answered
  1. Joe
    Joe avatar
    21 posts
    Registered:
    01 Dec 2010
    24 Jan 2014
    Link to this post
    I'd like to be able to manipulate server controls in a masterpage template from a MVC Widget, instead of the typical 'from a usercontrol' scenario.  Here's the code I have now, which gets me the controls on the page, but they only bring back controls that are in Placeholders, not controls that are on the masterpage itself.  Is there anyway to access the controls on the masterpage from an MVC Controller?

    Here's what I have so far in the MVC controller:
    Guid currentPageId = SiteMapBase.GetCurrentNode().PageId;
     PageManager pm = PageManager.GetManager();
     PageData pageData = pm.GetPageData(currentPageId);
     var masterTemplate = pageData.Template;
     
     foreach (var m in  masterTemplate.Controls)
     {
         //currently cycles through controls in placeholders only.
     }

    My masterpage looks like this, I'd like to be able to add or change attributes in the 'mainDiv' asp:Panel.
    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterwithLayoutWidgets.master.cs" Inherits="SitefinityWebApp.Content.Masters.MasterwithLayoutWidgets" %>
     
    <%@ Register TagPrefix="cc" TagName="Modernizr" Src="/CommonControls/ModernizrInclude.ascx" %>
    <%@ Register TagPrefix="cc" TagName="JQuery" Src="/CommonControls/JqueryInclude.ascx" %>
    <%@ Register TagPrefix="cc" TagName="KendoWeb" Src="/CommonControls/KendoWebInclude.ascx" %>
    <%@ Register TagPrefix="cc" TagName="SiteStyles" Src="/CommonControls/SiteStyles.ascx" %>
    <%@ Register TagPrefix="cc" TagName="PageHeader" Src="/CommonControls/Header.ascx" %>
    <%@ Register TagPrefix="cc" TagName="PageFooter" Src="/CommonControls/Footer.ascx"%>
    <!DOCTYPE html>
     
    <!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
    <!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
    <!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
    <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head id="Head1" runat="server">
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width" />
        <link rel="apple-touch-icon-precomposed" href="/Content/Images/apple-touch-icon-precomposed.png" />
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        <cc:Modernizr ID="Modernizr1" runat="server" />
        <cc:SiteStyles ID="SiteStyles1" runat="server" />
        <cc:JQuery ID="JQuery1" runat="server" />
        <cc:KendoWeb ID="KendoWeb1" runat="server" />
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="page">
            <header id="mainHeader">
                <cc:PageHeader id="PageHeader1" runat="server"></cc:PageHeader>
            </header>
            <asp:Panel ID="mainDiv" runat="server">
            <%--<div runat="server" id="main" role="main">--%>
                <asp:ContentPlaceHolder ID="optionalTopHeroContentPlaceholder" runat="server"></asp:ContentPlaceHolder>
                <asp:ContentPlaceHolder ID="optionalSubNavPlaceholder" runat="server"></asp:ContentPlaceHolder>
                <asp:ContentPlaceHolder ID="optionalBreadCrumbsPlaceHolder" runat="server"></asp:ContentPlaceHolder>
                <asp:contentplaceholder id="bodyPlaceholder" runat="server"/>
                <%--<div id="content" class="content col-2">
                    <section class="col-primary">
                        <asp:contentplaceholder id="leftSidePlaceholder" runat="server"/>
                    </section>
                    <aside id="sidebar" class="col-right">
                        <asp:ContentPlaceHolder ID="rightsideCrossSellPlaceholder" runat="server"></asp:ContentPlaceHolder>
                    </aside>
                </div>--%>
               <%-- <div id="relatedContent" class="row">
                    <div id="featuredRelated" class="modules-h col-4">--%>
                        <asp:ContentPlaceHolder ID="optionalRelatedSectionPlaceholder" runat="server"></asp:ContentPlaceHolder>
                   <%-- </div>
                </div>--%>
            <%--</div>--%>
            </asp:Panel>
            <cc:PageFooter id="PageFooter1" runat="server"></cc:PageFooter>
        </div>
        </form>
        <script type="text/javascript" src="/Content/Scripts/plugins.js"></script>
        <script type="text/javascript" src="/Content/Scripts/main.js"></script>
        <script type="text/javascript" src="/Content/Scripts/global.js"></script>
    </body>
    </html>
  2. Andrey
    Andrey avatar
    14 posts
    Registered:
    29 Sep 2016
    27 Jan 2014
    Link to this post
    Hi,

    In order to achieve your goal you could try to use the following approach:

        Guid currentPageId = SiteMapBase.GetCurrentNode().PageId;
        PageManager pm = PageManager.GetManager();
        PageData pageData = pm.GetPageData(currentPageId);
        var masterTemplate = pageData.Template;
     
        foreach (TemplateControl m in masterTemplate.Controls)
        {
            Control panelControl = FindMainDiv((Control)m, "mainDiv");
            if (panelControl != null)
            {
                Panel panel = panelControl as Panel;
                //panel.Controls.Add();
                //panel.Controls.Remove();
            }
        }
     
    }
     
    public Control FindMainDiv(Control root, string ID)
    {
        foreach (Control ctrl in root.Controls)
        {
            if(ctrl.Parent != null && ctrl.ID==ID)
            {
                return ctrl;
            }
            else if(ctrl.Parent != null)
            {
               Control control= FindMainDiv(ctrl.Parent, ID);
               if (control != null)
                   return control;
            }
        }
        return null;
    }


    Regards,
    Andrey
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Joe
    Joe avatar
    21 posts
    Registered:
    01 Dec 2010
    27 Jan 2014 in reply to Andrey
    Link to this post
    Ok i've tried implementing this, however I'm having trouble on this line with casting the TemplateControl as a System.Web.UI.Control:
    Control panelControl = FindMainDiv((Control)m, "mainDiv");
    The error is 'Cannot convert type 'Telerik.Sitefinity.Pages.Model.TemplateControl' to 'System.Web.UI.Control'.

    I'm assuming 'TemplateControl' is from 'Telerik.Sitefinity.Pages.Model.TemplateControl'
    I'm also assuming FindMainDiv takes and returns a 'System.Web.UI.Control'.  Are these assumptions right?
  4. Andrey
    Andrey avatar
    14 posts
    Registered:
    29 Sep 2016
    27 Jan 2014
    Link to this post
    Hi,

    Yes, in this case you may check this help article which describes how to add/remove controls from page.

    Regards,
    Andrey
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Joe
    Joe avatar
    21 posts
    Registered:
    01 Dec 2010
    27 Jan 2014
    Link to this post
    Specifically, I was just looking to add a class to the 'mainDiv' panel control from the MVC widget codebehind with something like this: 'mainDiv.Attributes.Add("style", myStyleshere);'

    So if I understand you correctly, I have to programatically remove and then re-add the panel control for this to work?
  6. Andrey
    Andrey avatar
    14 posts
    Registered:
    29 Sep 2016
    29 Jan 2014
    Link to this post
    Hello,

    Would it be possible to use content placeholder control instead of asp Panel control? This is needed because we do not provide currently access to controls different than placeholder controls.

    Regards,
    Andrey
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Jon Mikel Inza
    Jon Mikel Inza avatar
    3 posts
    Registered:
    20 Oct 2009
    03 Oct 2014 in reply to Andrey
    Link to this post

    Hi,

     Does Sitefinity 7.1 version allow to reach controls inside ASP.Net master pages but outside content place holders?

    Thanks in advance.

    Regards

     

    Jon Mikel

7 posts, 0 answered