Copied to clipboard

Flag this post as spam?

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


  • James Crane 34 posts 80 karma points
    Aug 10, 2009 @ 17:10
    James Crane
    0

    Accessing one macro from another

    Hi,

    I have a quickbasket macro (usercontrol) that has a property (int) and a method that writes this property out to a label. This works nicely on pageload.

    However, when another item is added I need to call this macro from another macro (again a usercontrol), set the property and call the method to write the new figure to the page.

    Traditionally I would have got a reference to this usercontrol thus:

    usercontrols_QuickBasket quickBasket = (usercontrols_QuickBasket)Page.FindControl("QuickBasket");

    quickbasket would then contain all the public methods and properties - however how can I achieve the same thing the umbraco way? Is there a findMacro type method?

    Many thanks

    JC

  • Chris Koiak 700 posts 2626 karma points
    Aug 10, 2009 @ 17:32
    Chris Koiak
    0

    What version of Umbraco are you using?

    If it's v4 then you can declare your UserControls directly on the template, therefore the above approach should still work.

    If that's not the case (and maybe a better solution) you could implement a Singleton based StateManager. This class would be updated by one control and then checked by the second control.

    For example, UserControl1 may have

    OnLoad()
    {
    StateManager.Instance.BasketItems = 5;
    }

    and UserControl2 would then

    OnPreRender()
    {
    If(StateManager.Instance.BasketItems > 0)
    {
    // Change rendering options
    }
    }

    Try and avoid controls/macros having to talk directly to each other. This generally means that they are very tightly coupled and if you change one there's a chance you may break something in another.

  • James Crane 34 posts 80 karma points
    Aug 10, 2009 @ 17:50
    James Crane
    0

    Thanks Chris,

    It is version 4, so I guess I could just use the user controls route. I did like your idea and I agree tightly coupling controls.

    Your solution still hits on the problem I'm currently suffering, that is I need to force the usercontrol to either reload or run the update method as it will always be one step behind the current basket, if you see what I mean - it will display the basket content from the previous post.

    I think maybe the solution is to just put a label directly into the master page to display it, and write a method I can call anywhere to update it - not as modular as I would like but it will work.

     

    Thanks for your help

    JC

  • Chris Koiak 700 posts 2626 karma points
    Aug 10, 2009 @ 18:02
    Chris Koiak
    101

    It depends on the position of your controls on the page along with when in the page lifecycle your updaing the labels value.

    For example, if your controls are in this order:

    <asp:YourLabelControl id="c1" runat="server"/> 

    <asp:YourSecondControl id="c2" runat="server" />

    then the load method of c1 will occur before the load method of c2.

    However the OnPreRender method of c1 will occur after the OnLoad method of c2. Therefore if you move the logic that sets the text of the label to the OnPreRender method then the value will have been updated by this point.

     

     

  • James Crane 34 posts 80 karma points
    Aug 10, 2009 @ 18:10
    James Crane
    0

    Thanks Chris - too tired to see that one.

    Working now - thanks again.

     

Please Sign in or register to post replies

Write your reply to:

Draft