Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Eddie Foreman 215 posts 288 karma points
    Oct 19, 2010 @ 12:45
    Eddie Foreman
    0

    Find Control in Content Place Holder

    Hi All

    From the masterpage page load method, I'm trying to test if it's possible to find a control in a content place holder of a general template.

    The masterpage has a content place of SiteMasterContentPlaceHolder, and the test label control has an id of "test".

     ContentPlaceHolder cp = Master.FindControl("SiteMasterContentPlaceHolder") as ContentPlaceHolder;
    Label lb = cp.FindControl("test") as Label;

    This returns System.NullReferenceException when attempting to locate the label control. 

    Thanks

    Eddie

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Oct 19, 2010 @ 13:05
    Jeroen Breuer
    1

    Here is a sample I'm using to find a macro from another macro:

    Control extension:

    /// <summary>
    /// Find a control and return it as T.
    /// This controls goes recursive through all controls until the control is found.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="root"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public static T FindControlRecursive<T>(this Control control, string controlID)
        where T : Control
    {
        //Find the control.
        Control foundControl;
        if (control != null)
        {
            foundControl = control.FindControl(controlID);
            if (foundControl != null)
            {
                //Return the Control as T.
                return foundControl as T;
            }
            foreach (Control c in control.Controls)
            {
                foundControl = c.FindControlRecursive<T>(controlID);
                if (foundControl != null)
                {
                    //Return the Control as T.
                    return foundControl as T;
                }
            }
        }
    
        return null;
    }

    The code to find the macro:

    //Get the header menu macro (another usercontrol) which is also on this page.
    Macro menuMacro = this.Page.FindControlRecursive<Macro>("headermenuMacro");
    if (menuMacro != null)
    {
        //Set the menu. This will be set to the selected location.
        ISetMenu<int> setMenu = (ISetMenu<int>)menuMacro.Controls[0];
        setMenu.SetMenu(LocationId);
    }

    I think you can use something like this to find the ContentPlaceHolder.

    Jeroen

  • Eddie Foreman 215 posts 288 karma points
    Oct 19, 2010 @ 14:42
    Eddie Foreman
    0

    Hi Jeroen,

    Thanks for the reply, I was hoping that I could just use the findcontrol method to grab the ContentPlaceHolder and then from the there get the control.  Your approach looks good, but I'm afraid my coding is not upto this level.

    Eddie

  • Matt Taylor 873 posts 2086 karma points
    Jul 18, 2011 @ 14:53
    Matt Taylor
    0

    Hi Jeroen,

    I hope you are listening to this thread and able to help me.

    I have two .Net macros on a page and within one macro I need to call a 'Refresh' method in the other.

    Within my template the macro I'm trying to find looks like this:

    <umbraco:Macro Alias="MiniBasket" runat="server"></umbraco:Macro>

    I've created the extension method you've shown above, help here for anyone that needs it.
    And I've added the following code to find the macro.

    macro menuMacro = Page.FindControlRecursive<macro>("MiniBasket");
    if (menuMacro != null)
    {
      MiniBasket.MiniBasket basket= (MiniBasket.MiniBasket)menuMacro.Controls[0];
      basket.Refresh();
    }

    There's a couple of things I'm not sure about.

    1. In Umbraco 4.7 the only macro object I could find has a lower case 'm' so am I using the right macro object?

    2. Am I passing in the correct ID of the control because I've not set the ID anywhere?

    Regards,

    Matt

     

  • Matt Taylor 873 posts 2086 karma points
    Jul 18, 2011 @ 15:13
    Matt Taylor
    0

    Played about with this some more and discovered the Macro object is in fact

    umbraco.presentation.templateControls.Macro

    I'm still not entirely sure what sets the ID of the control?

    Anyhow, it worked fine once I started looking for the corrcet Macro object.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jul 18, 2011 @ 17:49
    Jeroen Breuer
    0

    Hello,

    When you look for a control you look for the ID of it. Normally the Macro doesn't have an ID, but it is a webcontrol so you can just set it.

    For example: 

    <umbraco:MacroAlias="MiniBasket"runat="server" ID="Minibasket1"></umbraco:Macro>

     You can find this with the following code:

    umbraco.presentation.templateControls.Macro menuMacro =Page.FindControlRecursive<umbraco.presentation.templateControls.Macro>("MiniBasket1");
    if(menuMacro !=null)
    {
     
    MiniBasket.MiniBasket basket=(MiniBasket.MiniBasket)menuMacro.Controls[0];
      basket
    .Refresh();
    }

    Jeroen

  • Matt Taylor 873 posts 2086 karma points
    Jul 18, 2011 @ 17:55
    Matt Taylor
    0

    Thanks Jeroen,

    Got it all going.

Please Sign in or register to post replies

Write your reply to:

Draft