Copied to clipboard

Flag this post as spam?

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


  • suzyb 474 posts 932 karma points
    May 29, 2011 @ 16:34
    suzyb
    0

    Using datatype front end

    I am using the xpathcheckboxlist datatype to store a list of interests for members of my site in addition to some other data.  I'd like members to be able to edit their data including their interests in an edit profile section.

    I've created a user control that allows members to edit their name and other details and I'd like to use the xpathcheckboxlist datatype in this user control as well.  Is that possible?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jun 01, 2011 @ 09:46
    Lee Kelleher
    2

    Hi suzyb,

    Here's a quick code snippet of how to add a custom data-type control to your page.

    // get the property value (from the current node)
    var currentNode = umbraco.NodeFactory.Node.GetCurrent();
    var property = currentNode.GetProperty("interests");
    
    // get the data-type
    var factory = new umbraco.cms.businesslogic.datatype.controls.Factory();
    var dataType = factory.DataType(new Guid("d2d46927-f4f8-4b1b-add7-661cc09a0539"));
    
    // set the values
    dataType.DataTypeDefinitionId = 1234; // the data-type id
    dataType.DataEditor.Editor.ID = property.Alias;
    dataType.Data.Value = property.Value;
    
    // add the data-type editor control
    this.Controls.Add(dataType.DataEditor.Editor);

    Few things to keep in mind...

    • You'll need to find out the "DataTypeDefinitionId" of the data-type - do this by hovering your mouse cursor over the data-type and seeing what "id" is in your status bar.
    • My example adds the control to the page/user-control ... if you have a PlaceHolder or Panel, just change it to use that instead.
    • For the PostBack, you'll probably need to persist the data-type object, so set it to a private field and use that to save the new property value later on.

    Hopefully this is enough to get you going! :-)

    Cheers, Lee.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jun 01, 2011 @ 10:22
    Jeroen Breuer
    1

    Hi suzyb,

    Lee his sample is the best way to do it. Here you can find another sample: http://our.umbraco.org/wiki/reference/code-snippets/use-mntp-on-a-usercontrol

    Jeroen

  • suzyb 474 posts 932 karma points
    Jun 06, 2011 @ 10:13
    suzyb
    0

    Thanks for the replies.  I'll have a look when I get back on that part of the project.

  • Kenny Burns 173 posts 305 karma points
    May 03, 2012 @ 17:52
    Kenny Burns
    0

    Hi Guys,

    I was looking around the forum to see if this exact issue had been covered. I have the datatype appearing on the front end (a checkbox list) and pulling out the values using the example lee gave above - great stuff!

    The issue I am having is saving it back? Lee mentions "need to persist the data-type object, so set it to a private field" but I am totally lost! This is a new one for me so any help would be greatly appreciated! :)

    Thanks!

    Kenny

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    May 03, 2012 @ 18:02
    Jeroen Breuer
    0

    Normally Umbraco saves the data from a datatype on as a property on a document/node, but since you're not using it as a property you need to decide yourself where you want to store the data. It could be xml or a custom table or even save it with the Document API.

    Jeroen

  • Kenny Burns 173 posts 305 karma points
    May 03, 2012 @ 18:09
    Kenny Burns
    0

    Hi Jeroen,

    Thanks for that. If it helps the checkbox datatype I am using is a property of a member type. On the front end I would like to allow users to update those checkboxes and I am dynamically creating the control (checkbox datatype) in a user control. I think I may have to look into viewstate.

    Kenny

  • Kenny Burns 173 posts 305 karma points
    May 03, 2012 @ 18:27
    Kenny Burns
    0

    Hi Guys,

    I managed to solve this in case anyone else needs it - not sure if it is the best way to do it but it works for me! :)

    On Page Load - dynamically add the control:

     

         //add the checkboxes to front end
                // get the property value (from the current node)
                var currentNode = umbraco.NodeFactory.Node.GetCurrent();
                //var property = currentNode.GetProperty("interests");
     
                 var theMem = Membership.GetUser();
                 Member m2 = new Member((int)theMem.ProviderUserKey);
                 var property = m2.getProperty("test");
     
                // get the data-type
                var factory = new umbraco.cms.businesslogic.datatype.controls.Factory();
                var dataType = factory.DataType(new Guid("b4471851-82b6-4c75-afa4-39fa9c6a75e9"));
     
                // set the values
                dataType.DataTypeDefinitionId = 3132; // the data-type id
                dataType.DataEditor.Editor.ID = "checkBoxList";
                dataType.Data.Value = property.Value;
     
                // add the data-type editor control
                checkboxes.Controls.Add(dataType.DataEditor.Editor);
     

    On Save Event - save values fromn control:

     
    //store checkbox list values as string and update member property type
                    string checkeditems = "";
                    CheckBoxList myCheckboxes = (CheckBoxList)FindControl("checkBoxList");
                    foreach (ListItem li in myCheckboxes.Items)
                    {
                        if (li.Selected)
                        {
                            checkeditems += li.Value + ",";
     
                        }
                    }
                    if (checkeditems != "")
                    {
                        checkeditems = checkeditems.Remove(checkeditems.Length - 1, 1);
                    }
     
                    m.getProperty("test").Value = checkeditems;

                   

                    m.Save();

     

Please Sign in or register to post replies

Write your reply to:

Draft