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?
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.
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! :)
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.
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.
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;
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?
Hi suzyb,
Here's a quick code snippet of how to add a custom data-type control to your page.
Few things to keep in mind...
Hopefully this is enough to get you going! :-)
Cheers, Lee.
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
Thanks for the replies. I'll have a look when I get back on that part of the project.
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
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
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
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:
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;is working on a reply...