I'm trying to make a checkbox list on my page in Razor. I need it to list all the prevalues from the datatype and then set any checkbox that's checked in the backend to checked. I'm quite close to getting it to work at the moment, but there's just a flaw that I can't figure out.
My code so far:
@using System.Xml.XPath
@using System.Web.UI.WebControls
@using umbraco.cms.businesslogic.web
@{
int nodeId = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
Document doc = new Document(nodeId);
if(doc != null)
{
<ul>
@{
// Get prevalues from the class datatype
XPathNodeIterator preValueRootElementIterator =
umbraco.library.GetPreValues(1100);
preValueRootElementIterator.MoveNext(); //move to first
XPathNodeIterator preValueIterator =
preValueRootElementIterator.Current.SelectChildren("preValue", "");
foreach(var value in doc.getProperty("klasse").Value.ToString().Split(','))
{
while (preValueIterator.MoveNext())
{
if(preValueIterator.Current.GetAttribute("id", "") == value)
{
<input type="checkbox" checked="yes"
value='@preValueIterator.Current.GetAttribute("id", "")'>
@preValueIterator.Current.Value
</input>
}
else
{
<input type="checkbox"
value='@preValueIterator.Current.GetAttribute("id", "")'>
@preValueIterator.Current.Value
</input>
}
}
}
}
</ul>
}
}
It checks off the first value from my document property, but not the rest. Also, I find it not-so elegant to have the while loop inside the foreach. Is there a better way to do this?
protected override void OnInit(EventArgs e)
{ //Get the function DataType, set the properties and add it to the PlaceHolder.
DataTypeDefinition functionDataType = DataTypeDefinition.GetDataTypeDefinition(functionDataTypeId);
_ddlFunction = (umbraco.editorControls.dropdown)functionDataType.DataType.DataEditor;
_ddlFunction.ID = "DdlFunction";
_ddlFunction.CssClass = "dropdown";
PlaceHolderFunction.Controls.Add(_ddlFunction); }
This way the datatype which you normally add to a documenttype can now be placed on a usercontrol which can be used as a macro in the website.
Thanks for your input! I did it another way, though yours also works great :)
The (rather clumsy) way I did it was to get all the prevaules with the XPathNodeIterator and split them into a stringarray. The same with the nodes prevalues and then loop through the datatype prevalues array with an if(string.Contains(string)) statement.
Checkbox list prevalues problem
Hi all,
I'm trying to make a checkbox list on my page in Razor. I need it to list all the prevalues from the datatype and then set any checkbox that's checked in the backend to checked. I'm quite close to getting it to work at the moment, but there's just a flaw that I can't figure out.
My code so far:
It checks off the first value from my document property, but not the rest. Also, I find it not-so elegant to have the while loop inside the foreach. Is there a better way to do this?
Any hint/help is appreciated! :-)
Thanks a lot in advance,
Bo
Oh, please ignore the reference to System.UI.WebControls - that's from a previous experiment ;-)
This is another way that works for me. Not Razor related, but it might help you.
I add the datatype (with it's prevalues) to the page as a webcontrol. More info about that is in this topic: http://our.umbraco.org/forum/developers/extending-umbraco/6863-Datatype-on-normal-page-or-UserControl
Here is a dropdown sample:
The webforms placeholder:
The code behind:
This way the datatype which you normally add to a documenttype can now be placed on a usercontrol which can be used as a macro in the website.
If you use this for example as an input field for seaching, than you can still output the search results in Razor. See this topic: http://our.umbraco.org/forum/developers/razor/21722-Set-Razor-macro-parameters-in-code-behind#comment81570
Jeroen
Hi Jeroen,
Thanks for your input! I did it another way, though yours also works great :)
The (rather clumsy) way I did it was to get all the prevaules with the XPathNodeIterator and split them into a stringarray. The same with the nodes prevalues and then loop through the datatype prevalues array with an if(string.Contains(string)) statement.
The code:
string dataTypePreValues = string.Empty;
while(preValueIterator.MoveNext())
{
dataTypePreValues += preValueIterator.Current.Value + ",";
}
string[] dataTypePreValuesArray = dataTypePreValues.Split(',');
string[] nodePreValuesArray = node.klasse.Split(',');
foreach(string s in dataTypePreValuesArray)
{
if(nodePreValuesArray.Contains(s))
{
<p><span>Found: @s</span></p>
}
else
{
<p><span>Not found: @s</span></p>
}
}
Thanks again!
- Bo
is working on a reply...