Copied to clipboard

Flag this post as spam?

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


  • Nick 8 posts 28 karma points
    Jan 12, 2010 @ 23:13
    Nick
    0

    What is the simplest way to write umbraco items from the code behind ?

    Hello,

    I've been trying to use a Literal to dynamically change the field of an umbraco item, however it doesn't display.

    Liberal.Text =  "<umbraco:Item runat=\"server\" field=\"title\" />";

    If I do Liberal.Text =  "lol"; or even  Liberal.Text =  "<a href=\"address\">link</a>"; it displays fine, but it doesn't work with the umbraco item.

    Putting <umbraco:Item runat="server" field="title" /> in the page also work so the umbraco item is working, the problem is with the liberal.

     

    I would like to change the field string in the code behind depending on the language parameter in the url, with something like:

    string str = "title_" + Request.Params.Get("lang");
    string txt = "<umbraco:Item ID=\"Item1\" runat=\"server\" field=\"" + str + "\" />";
    Liberal.Text = txt;

    So I could get field="title_en" or field="title_fr" in the umbraco item.

     

    Does anyone have a simple way to display umbraco items with parameters that you can change in the code behind file of a masterpage ?

     

    Thanks alot in advance

     

  • Frederik Vig 29 posts 61 karma points
    Jan 12, 2010 @ 23:30
    Frederik Vig
    1

    <umbraco:item> is a server side control. same as the Literal control. To set your literals text property, you could do something like this:

    var currentPage = Node.GetCurrent();

    myLiteral.Text = currentPage.GetProperty("title" + Request.QueryString["lang"]);

    Though remember to at least HtmlEncode the QueryString before doing something like that.

    HttpUtility.HtmlEncode(Request.QueryString["lang"])
  • Nick 8 posts 28 karma points
    Jan 12, 2010 @ 23:45
    Nick
    0

    Ah, so I can't "echo" a control in a literal ? I get it now why it didn't work.

    To try your Node.GetCurrent();

    What "using" do I have to reference in the code ?

     

    Also thanks for the help, I'm a computer science student :)

  • Nick 8 posts 28 karma points
    Jan 13, 2010 @ 00:07
    Nick
    0

    And while I'm at it, putting aside my initial issue with the Liberal,

    how do YOU guys render umbraco controls dynamically anyway ?

    What's the "pro" way to do it ?

  • Jesper Hauge 298 posts 487 karma points c-trib
    Jan 13, 2010 @ 00:09
    Jesper Hauge
    0

    Hello Nick, and welcome to Umbraco

    The Node class is found in the umbraco.presentation.nodeFactory which is what you should write in your using statement, this namespace is in the core umbraco assembly (umbraco.dll)

    The umbraco assembly also contains a library class, that is used a lot in xslt files, and is also availabe for codebehinds, it contains a lot of different static method that can be useful, like the GetItem(string alias) method. This method returns a named attribute or property on the current page (or even another page if you use the GetItem(int nodeId, string alias) overload. With this you can do things like:

    using umbraco;
    
    // namespace and class declarations
    
    string title = library.GetItem("title");

    In a cs-codebehind or:

    <%@ Import Namespace="umbraco"%>
    
    <asp:Literal runat="server" Text='<%= library.GetItem("header") %>' />
    

    I recommend downloading Red Gate's .NET Reflector and use that for opening the umbraco dlls, and check out the code. You can even use it to search for the Node class and find it, once the umbraco.dll is opened.

    Regards
    Jesper Hauge

  • Nick 8 posts 28 karma points
    Jan 13, 2010 @ 09:50
    Nick
    0

    Wow that looks amazing, thanks.

     

Please Sign in or register to post replies

Write your reply to:

Draft