Copied to clipboard

Flag this post as spam?

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


  • Mike Chambers 621 posts 1203 karma points c-trib
    Aug 20, 2010 @ 13:43
    Mike Chambers
    0

    Calling a method on a usercontrolMacro from another control.

    I'm basically creating a PageBasket, eg button on the page click on it and it adds that page to a Basket.

    So I have on usercontrol with the "add to button" to be positioned in the content, and a second usercontrol with the addto method that this button calls.

    If I register the control in the template and use a recursive FindControl to find the basket then all works find.

    eg

    <%@ Register TagPrefix="uc1" TagName="PdfBasket" Src="/usercontrols/PdfBasket.ascx" %>

     <uc1:PdfBasket ID="PdfBasket" runat="server" />

    Control mySide = FindControlRecursive(this.Page, "PdfBasket");

                if (mySide != null)
                {
                   
    PdfBasket pdfB = (PdfBasket)mySide;
                    PdfBasket.AddBasketItem(1, "Mike"); // this is the add page method on the Basket
                    pdfB.UpdateBasket(); // this is the update method on the Basket to show what I just added

                  }

    However, if I want to allow the basket to be positioned by the designer in the content and not in the template... I need to go with a usercontrol macro.

    so I setting up the macro and inserted... resulting in

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

    Now if I want to take the same approach, in order to be able to find the control I need to add an id..

    <umbraco:Macro Alias="PdfBasket" runat="server" id="PdfBasket"></umbraco:Macro> (I sort of assumed that the alias would result in the id being automatically created by the umbraco core, but that doesn't seem to be the case)

    However now when I try to cast the found object to my PdfBasket Object.. I get an invalid castexception as it seems the maco object is an umbraco.presentation.templateControls.Macro object. So my question is... How do I get at my PdfBasket usercontrol object that must be residing in the umbraco.presentation.templateControls.Macro object???

  • Mike Chambers 621 posts 1203 karma points c-trib
    Aug 20, 2010 @ 13:46
    Mike Chambers
    0

    The edit functionality on the forum seems to be misbehaving...

    So just to add, I am using Umbraco 4.5.1 (in a medium trust environment if that makes a difference)

  • Mike Chambers 621 posts 1203 karma points c-trib
    Aug 20, 2010 @ 13:57
    Mike Chambers
    0

    Isn't it always the case... another 5mins thinking or writing the question you have an epiphany....

     

    umbraco.presentation.templateControls.Macro m = (umbraco.presentation.templateControls.Macro)FindControlRecursive(this.Page, "PdfBasketMacro");
                foreach (Control innerCtrl in m.Controls)
                {
                    try
                    {
                        PdfBasket pdfB = (PdfBasket)innerCtrl;
                        PdfBasket.AddBasketItem(1, "Mike");
                        pdfB.UpdateBasket();
                    }
                    catch {}
                }

    Not sure if this is as elegant as a m.findControl("PdfBasket") for instance, but can't see a way to specify what id the usercontrol within the macro will end up with.. in my instance it seems to be PdfBasket_8 

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies