Copied to clipboard

Flag this post as spam?

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


  • Mike Taylor 155 posts 353 karma points
    Aug 11, 2011 @ 16:31
    Mike Taylor
    0

    User control macro on master page - how to access its properties from another user control?

    I have a "StandardPage" master page, and I've placed a macro on it which points to a user control "Message.ascx". Let's say this control contains an asp:Label control.

    On a different master page, which uses StandardPage as *its* master page, I have another macro which points to another separate user control. I need this second control to be able to set the Text property of the asp:Label on the first user control.

    Is this do-able?

    Mike

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 11, 2011 @ 17:01
    Jeroen Breuer
    0

    Yes this is possible. You can fetch another usercontrol and do stuff with that. Here is a sample using macro's:

    Recursive method to find the control:

    /// Find a control and return it as T.
    /// This controls goes recursive through all controls until the control is found.
    public static T FindControlRecursive(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(controlID);
                if (foundControl != null)
                {
                    //Return the Control as T.
                    return foundControl as T;
                }
            }
        }
    
        return null;
    }

    Get the macro that has ID="headermenuMacro".

    //Get the header menu macro (another usercontrol) which is also on this page.
    Macro menuMacro = this.Page.FindControlRecursive<Macro>("headermenuMacro");

    On that you can do FindControl or FindControlRecursive to get the control that you need :).

    Jeroen

  • Mike Taylor 155 posts 353 karma points
    Aug 11, 2011 @ 17:13
    Mike Taylor
    0

    Thanks Jeroen, but the initial call to FindControlRecursive is failing because it needs two parameters - do I pass null in as the first parameter?

    Mike

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 11, 2011 @ 17:20
    Jeroen Breuer
    0

    It's an extension method so you don't need to use the first parameter :). If you want to use it as a normal method you need to do this: 

    //Get the header menu macro (another usercontrol) which is also on this page.
    Macro menuMacro =FindControlRecursive<Macro>(this.Page, "headermenuMacro");

     Jeroen

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 11, 2011 @ 17:24
    Bo Damgaard Mortensen
    0

    Hi Mike,

    The method Jeroen pasted is an extension method, you can read everything about it here at Scott Gu's blog: http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx

    So bascially you call would look something like: yourControl.FindControlRecursive("controlID"); where Type defines the type you're looking for.

    Correct me if i'm wrong here, Jeroen.

    / Bo

    Edit: Way ahead of me, Jeroen ;)

  • Mike Taylor 155 posts 353 karma points
    Aug 11, 2011 @ 17:38
    Mike Taylor
    0

    Hmm. Still not getting anything (slightly out of my comfort zone...) Does this Recursive method account for the fact that the control is on a different master page? I have this master page structure:

    Master.master --> StandardPage.master --> MyPage.master

    On StandardPage.master, I have a macro with alias of "PageMessage" which points to a user control called "Message.ascx". All this has on it is an asp:Label, which I want to use as a generic control to provide the user with feedback.

    On MyPage.master (which has StandardPage.master as its master page), I have a second macro which points to a different user control. From this control, I need to access the properties of the label on the first user control.

    Thanks

    Mike

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 11, 2011 @ 17:43
    Bo Damgaard Mortensen
    0

    Hmm, with all respect to Jeroens great codesnippet, I would think it might be a better job for jQuery? Simply just make a javascript document which acts like the "connector" on control X's events to manipulate the attributes of the Y (your label)

    Just a suggestion, really :)

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 11, 2011 @ 17:48
    Bo Damgaard Mortensen
    0

    Something along the lines this javascript object:

    var MyConnector = function() { 
    function manipulateLabel() {
    // Parse HTML tree to find your label (span) control and manipulate it
    $('.yourLabel').text("Hello World");
    }

    return {
    manipulateLabel : manipulateLabel
    };
    }();

    From any event on Control X you can call this with: onclick="MyConnector.manipulateLabel();"

    Edit: sorry for the code layout (or lack of it..), never been good friends with the code snippet here in out ;)

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Aug 11, 2011 @ 17:49
    Jeroen Breuer
    0

    @Bo jQuery could work, but that depends on what you need to do with the data.

    @Mike If you debug is there anything in marco you get from FindControlRecursive? This should work:

    //Get the header menu macro (another usercontrol) which is also on this page. 
    umbraco.presentation.templateControls.Macro menuMacro = FindControlRecursive<umbraco.presentation.templateControls.Macro>(this.Page, "headermenuMacro");

    Label label = FindControlRecursive
    menuMacro, "labelId");

    Remember that this method searches for an ID. Your macro doens't have this by default. You shouldn't search for the Alias which you normally set.

    Jeroen

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 11, 2011 @ 17:54
    Bo Damgaard Mortensen
    0

    True, Jeroen, however it seems like the label "just" acts as a generic response to any clients action. Honestly, I don't know what is more effecient, the generic recursive method or the javascript object :)

  • Mike Taylor 155 posts 353 karma points
    Aug 11, 2011 @ 17:58
    Mike Taylor
    0

    Well, I'm obviously doing something wrong with your code, Jeroen, because I found this (very similar) post by Lee: http://www.blogfodder.co.uk/2010/12/3/access-site-wide-user-message-box-from-umbraco-macro and it works fine.

    Label lblMessage = (Label)General.FindControlRecursive(Page, "lblMessage");
    lblMessage.Text = "This is my message";

Please Sign in or register to post replies

Write your reply to:

Draft