Follow me on twitter @yodasmydad
Ahhhh #Fringe can't finish like that!! Latest Tweet:

Hey I'm Lee. My blog was put up to house my strange thoughts, ramblings, nuggets of information I can refer back to and document my learning curves on new dev stuff like Umbraco v5 and other .NET related things.

All thoughts and comments on here are my own, and in no way reflect my employer - I also take no responsibility for spelling, grammar or terminology, so read at your own risk!

Blogs I Read

Sites I Like

Access Site Wide User Message Box From Umbraco Macro

If you are like me when building your Umbraco sites, having structured Master pages (Templates) and DocTypes is a must (In fact I'm sure I have an OCD about it!).  Personally for me it makes extending / making changes to a large site easier in the long run, and makes it easier to get specific content when querying with XSLT / Linq2Umbraco.

masterpages-depth

The only slight issue I found recently with a quite heavily nested masterpage structure, was if I needed to access other usercontrols on on a nested page from another Macro (.NET Usercontrol) it was sometimes hit or miss to find it easily.  And I had this exact same problem on the current project I am working on.

I wanted to have a nice UI Message Box which I could relay Update, Error or Info messages to to users from any of my custom Macros (All the Macro's I refer to are just normal usercontrols). For example logging in status, updating profile and search result messages etc...  I found this brilliant MessageBox usercontrol a while back on a website written by a chap called Janko, and really wanted to use it as it fitted my need perfectly (And I think looks pretty cool).

messageboxes

I didn't want to just add the MessageBox in the Macros themselves, because the macros were positioned in different places on different pages and sometimes multiple macros per page - Meaning the message box would appear in different places on the page, and in different sizes etc…  Something I thought would just look… well… shit.

The best option would be to add the MessageBox user control to the main masterpage and reference it from my Macros, so I added the MessageBox usercontrol right above my main ContentPlaceHolder (I registered it in my web.config to use the tag <msg:messagebox>).

<msg:messagebox ID="msgBox" ShowCloseButton="true" runat="server" Visible="false" />

<asp:ContentPlaceHolder ID="mastermaincontent" runat="server" />

But I started to stumble into annoying problems with Finding the control dependant on where the page was in the nested hierarchy.  After a quite a bit of Google'ing and not finding a suitable answer, I stumbled in this excellent little method on StackOverflow.

        /// <summary>
        /// Recursive FindControl method, to search a control and all child
        /// controls for a control with the specified ID.
        /// </summary>
        /// <returns>Control if found or null</returns>
        public static Control FindControlRecursive(Control root, string id)
        {
            if (id == string.Empty)
                return null;

            if (root.ID == id)
                return root;

            foreach (Control c in root.Controls)
            {
                var t = FindControlRecursive(c, id);
                if (t != null)
                {
                    return t;
                }
            }
            return null;
        }

I don't think it needs any explanation, but in essence loops through all the controls from the highest level until it finds the control you are after.  I guess its not the most efficient method, but it has helped me out no end and made my sitewide message box work perfectly.

I added the method above into my static Helper class and then just called it from any of my Macros like so (Passing in Page as the 'Control' variable) casting the found control as MessageBox.

var msgBox = (MessageBox)Helpers.FindControlRecursive(Page, "msgBox");

Obviously with 'msgBox' being the ID of my MessageBox control in the main masterpage, and then I could access the Message types in the usercontrol like

msgBox.ShowError("My Error Message");
msgBox.ShowInfo("My Info Message");
msgBox.ShowSuccess("My Success Message");

Note: I read the only issue this method might have is if you have multiple controls with the same ID. But using the above has worked out really well so far, and it seems to be very quick no matter what depth the page is.

Hope this helps someone else :)

Back to top