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
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.
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.
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?
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? :-)
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:
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):
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
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
Great idea Chris, haven't thought about that.
Will try this immediately and post the results here :)
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
Great work Chris! It works :D
Here's my code for anyone with the same situation:
Like I said before: this.GetCurrentLanguage() just returns the language, like "nl" or "en".
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
Thanks for the heads up Dirk.
I didn't know it worked that way, will use Node from now on :)
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.
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
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
is working on a reply...