I was able to create a custom control datatype from the dictionary by creating a .ascx control. This way, my client just needs to add an item to the dictionary and all dropdowns will reflect with the new dictionary item.
Some of my pages will need to supply this field so I made it mandatory. My problem is everytime I edit the page that has this custom datatype, it doesn't retain the value previously picked.
Ex. I have a doctype named sports page and user has to supply the sport type from a dropdown. This drop down was created from my custom control. Anyway, I created a page called Basketball. It saves the value when i click save and publish. But everytime i revisit this page to edit, i have to supply the sport type again.
It has to come from the dictionary. I have pages that is also using this scenario but not as a property but as an actual form in the page.
Maybe this will help:
In the database, the datatype prevalue is "~/usercontrols/customdatatypes/SportsType.ascx" while other controls have text instead of a path to the user control.
Also the xml in the database is saved as: <sportType><![CDATA[Badminton]]></sportType>
What does your UserControls looks like? It should have a property with "get" and "set". When you save the "get" is used to store the selected value in the database. The "set" is used when you open the Umbraco page so you can show the selected value from the database.
You shouldn't do that in the Page_Load method. Does your UserControl has the umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor interface? See this blog: http://www.nibble.be/?p=24
You need to do your stuff in:
public object value
{
get
{
//Save value
}
set
{
//Display value
}
}
I already have it in the set{} area. Take a look at the first code I pasted. The code I tested was only a test if it was doing anything in the Page_Load event. My user control already implements the IUsercontrolDataEditor interface. I will test out what you just gave and will let you know. There isn't much difference with I have though.
Custom Dropdown from Dictionary
I was able to create a custom control datatype from the dictionary by creating a .ascx control. This way, my client just needs to add an item to the dictionary and all dropdowns will reflect with the new dictionary item.
Some of my pages will need to supply this field so I made it mandatory. My problem is everytime I edit the page that has this custom datatype, it doesn't retain the value previously picked.
Ex. I have a doctype named sports page and user has to supply the sport type from a dropdown. This drop down was created from my custom control. Anyway, I created a page called Basketball. It saves the value when i click save and publish. But everytime i revisit this page to edit, i have to supply the sport type again.
It has to come from the dictionary. I have pages that is also using this scenario but not as a property but as an actual form in the page.
Maybe this will help:
In the database, the datatype prevalue is "~/usercontrols/customdatatypes/SportsType.ascx" while other controls have text instead of a path to the user control.
Also the xml in the database is saved as: <sportType><![CDATA[Badminton]]></sportType>
Please help. Thank you.
What does your UserControls looks like? It should have a property with "get" and "set". When you save the "get" is used to store the selected value in the database. The "set" is used when you open the Umbraco page so you can show the selected value from the database.
Jeroen
publicobject value
{
get
{
return ddlSports.SelectedValue;
}
set
{
if (value != null)
{
ddlSports.SelectedValue = value.ToString();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
umbraco.cms.businesslogic.Dictionary.DictionaryItem parent = new umbraco.cms.businesslogic.Dictionary.DictionaryItem("SportsEvents");
foreach (var child in parent.Children)
{
string item = umbraco.library.GetDictionaryItem(child.key);
ddlSports.Items.Add(new ListItem(item, item));
}
}
}
It's probably because ddlSports.SelectedValue = value.ToString(); doesn't work.
Have a look at these topics:
http://stackoverflow.com/questions/5121639/how-to-programatically-set-selectedvalue-of-dropdownlist-when-it-is-bound-to-xml
http://forums.asp.net/t/1004541.aspx
Jeroen
Hello Joroen,
I tested if it gets anything from the database when it loads:
if (!Page.IsPostBack)
{
umbraco.cms.businesslogic.Dictionary.DictionaryItem parent = new umbraco.cms.businesslogic.Dictionary.DictionaryItem("SportsEvents");
foreach (var child in parent.Children)
{
string item = umbraco.library.GetDictionaryItem(child.key);
ddlSports.Items.Add(new ListItem(item, item));
}
if (this.value != null)
{
ddlSports.Items.Add(new ListItem(this.value.ToString(), this.value.ToString()));
}
else {
ddlSports.Items.Add(new ListItem("---","---"));
}
}
It is not returning null. But I get an emptry string. Therefore, it doesn't attempt to get the value from the database in the first place.
You shouldn't do that in the Page_Load method. Does your UserControl has the umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor interface? See this blog: http://www.nibble.be/?p=24
You need to do your stuff in:
Jeroen
Hello Jeroen,
I already have it in the set{} area. Take a look at the first code I pasted. The code I tested was only a test if it was doing anything in the Page_Load event. My user control already implements the IUsercontrolDataEditor interface. I will test out what you just gave and will let you know. There isn't much difference with I have though.
Thank you
How do i click unsolved. this isn't solved yet.
Hello Jeroen,
I found a solution. http://our.umbraco.org/forum/developers/extending-umbraco/4230-Persisting-DropDownList-on-PostBack-(in-custom-sectiontree-page)
Everything I had in Page_Load was moved to Page_Init. No need to test if Page Posted back or not.
Therefore, i now have a dropdown with items from the dictionary. All the user has to do is add new items in that dictionary and all is well! Yey!.
Thank you,
Denise
is working on a reply...