In an ongoing project I am working on I needed to adapt a custom user control that was being used as a DataType in Umbraco. I needed to locate the nearest node that had a specific property set and default the selected value of my control to the value of the property in the parent document.
Here is the final piece of code:
[code language="c#"]//- Get the current document as our starting point
Document currentDoc = new Document(Convert.ToInt32(Request.QueryString["id"]));
//- Climb the tree looking for a parent node with the property set
while (currentDoc.getProperty("myProperty") == null)
{
currentDoc = new Document(currentDoc.Parent.Id);
}
//- Set the value in the list if it exists
if (this.myDropDownList.Items.FindByValue(currentDoc.getProperty("myProperty").Value.ToString()) != null)
{
this.myDropDownList.Items.FindByValue(currentDoc.getProperty("myProperty").Value.ToString()).Selected = true;
this.myDropDownList.Enabled = false; //-Disable the control so the branch can't be changed
}[/code]
Thanks again to Tim Geyssens for the tip that led me to my final solution.