Copied to clipboard

Flag this post as spam?

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


  • denisedelbando 141 posts 339 karma points
    Sep 04, 2013 @ 17:15
    denisedelbando
    0

    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.

     

     

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 05, 2013 @ 14:34
    Jeroen Breuer
    0

    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

  • denisedelbando 141 posts 339 karma points
    Sep 05, 2013 @ 14:59
    denisedelbando
    0

    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));

                    }

                }

          }

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 05, 2013 @ 15:06
    Jeroen Breuer
    0

    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

  • denisedelbando 141 posts 339 karma points
    Sep 05, 2013 @ 15:22
    denisedelbando
    0

    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. 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 05, 2013 @ 15:29
    Jeroen Breuer
    0

    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
                }
            }

    Jeroen

  • denisedelbando 141 posts 339 karma points
    Sep 05, 2013 @ 15:47
    denisedelbando
    100

    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

  • denisedelbando 141 posts 339 karma points
    Sep 05, 2013 @ 15:57
    denisedelbando
    0

    How do i click unsolved. this isn't solved yet. 

  • denisedelbando 141 posts 339 karma points
    Sep 05, 2013 @ 16:05
    denisedelbando
    1

    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

     

Please Sign in or register to post replies

Write your reply to:

Draft