Copied to clipboard

Flag this post as spam?

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


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Dec 01, 2009 @ 14:12
    Stefan Kip
    0

    Change Umbraco Item's Field property in code-behind

    Hi all!

    I'm working on a multilanguale website. So I have some properties on a page, like the following one:

    <umbraco:Item runat="server" Field="subTitle" />

    What I would like to do, is loop through the controls and check if it's an Umbraco Item, then add "_en" or "_de" for the English or German text respectively.
    I have the following code, where Controls.All() is an extension method, which returns all controls recursively (so grandchildren will be included too):

    Document objCurrentDocument = new Document(Node.GetCurrent().Id);

    foreach (Control objControl in Controls.All().Where(x => x is Item))
    {
    Item objItem = objControl as Item;
    if (objItem != null)
    {
    objItem.DebugMode = true;
    string sNewField = string.Format("{0}_{1}", objItem.Field, this.GetCurrentLanguage());
    if (objCurrentDocument.getProperty(sNewField) != null && objCurrentDocument.getProperty(sNewField).Value != null && !string.IsNullOrEmpty(objCurrentDocument.getProperty(sNewField).Value.ToString()))
    {
    objItem.Field = sNewField;
    }
    }
    }

    The problem is; I can change the Field property, and when in debug mode it shows the correct Field property value (e.g. subTitle_de), but the original backend value is displayed (subTitle's value instead of subTitle_de's value, where subTitle's value is the Dutch text).

    I saw the Umbraco Item's OnInit is executed before the masterpage's OnInit, and I guess the value for the property is pulled from the DB in the OnInit. (I looked at the source, but couldn't find it...).

    Btw. when creating an Umbraco Item in code-behind and adding it to, for example, a Label, it does display the correct value.

    Could anyone please help me with this? And to make things worse, it's kinda important and

  • Chris Koiak 700 posts 2626 karma points
    Dec 01, 2009 @ 14:26
    Chris Koiak
    1

    Hi,

    You could try extending umbraco:Item and create your own control. This way you would have better access to modify the operation of 'Item', it'd be more efficent too as you wouldn't need to loop through your controls.

    This approach would hopefully get round any load lifecycle issue too.

    Cheers,

    Chris

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Dec 01, 2009 @ 14:37
    Stefan Kip
    0

    Great idea Chris, haven't thought about that.

    Will try this immediately and post the results here :)

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 01, 2009 @ 15:14
    Dirk De Grave
    1

    Or roll your own ExpressionBuilder. I've never bookmarked those references (I should have), but a search on umbraco and ExpressionBuilder should reveal some links.

     

    Cheers,

    /Dirk

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Dec 01, 2009 @ 15:17
    Stefan Kip
    1

    Great work Chris! It works :D

    Here's my code for anyone with the same situation:

    namespace InfoCaster.Umbraco
    {
    [DefaultProperty("MLField")]
    [ToolboxData("<{0}:Item runat=\"server\"></{0}:Item>")]
    [Designer(typeof(ItemDesigner))]
    public class Item : umbraco.presentation.templateControls.Item
    {
    [Bindable(true)]
    [Category("Umbraco")]
    [DefaultValue("")]
    [Localizable(true)]
    public string MLField
    {
    get { return Field; }
    set
    {
    Document objCurrentDocument = new Document(Node.GetCurrent().Id);
    string sNewField = string.Format("{0}_{1}", value, this.GetCurrentLanguage());
    if (objCurrentDocument.getProperty(sNewField) != null && objCurrentDocument.getProperty(sNewField).Value != null && !string.IsNullOrEmpty(objCurrentDocument.getProperty(sNewField).Value.ToString()))
    {
    value = sNewField;
    }
    Field = value;
    }
    }
    }
    }

    Like I said before: this.GetCurrentLanguage() just returns the language, like "nl" or "en".

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 01, 2009 @ 15:48
    Dirk De Grave
    2

    I might be wrong here, but wouldn't that be some db overhead. You're using the document api which will query the db 1x for the ctor and 3x for each getProperty call? 

    Node.GetCurrent() will get the node from xml cache, so there's no need to use the Document ctor?

     

    Cheers,

    /Dirk

     

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Dec 01, 2009 @ 16:17
    Stefan Kip
    0

    Thanks for the heads up Dirk.

    I didn't know it worked that way, will use Node from now on :)

  • Thomas 2 posts 22 karma points
    Oct 20, 2010 @ 22:31
    Thomas
    0

    Hi all,

    I'm a newbie in Umbraco.

    Could you tell me how to Integrate this peace of code in Umbraco please?

    Do I need to integrate it in a DLL or something else?

     

    Chears.

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 20, 2010 @ 22:40
    Jan Skovgaard
    0

    Hi Thomas

    You need to create a project in visual studio and add a user control and then build it and copy the user control to the user control folder and the generated .dll file into the bin folder.

    But are you sure youd need this piece of code to achieve your goal? Maybe it could be achieve it using an XSLT macro? :-)

    /Jan

  • Thomas 2 posts 22 karma points
    Oct 20, 2010 @ 22:46
    Thomas
    0

    Hi jan,

     

    I know I can do it in XSLT but my team doesn't want to use XSLT, so that's why I'm interested by this kind of solution.

    Thanks a lot for you quick answer.

     

    /Thomas

Please Sign in or register to post replies

Write your reply to:

Draft