Developer forum

Forum » Development » SelectionBox not working

SelectionBox not working

Tomas Gomez
Reply

Hi,
I need to implemented a custom ProductFieldTypeProvider to select and sort several ID values from a custom list. The Extending Ecommerce page shows an example with a DropDownList, but no multiselection and no customizable sorting are available with DropDownList. Changing the DropDownList for a SelectionBox results in no saved values (ie. the user selects the values, but no values are saved aferwards). 

I don't know if there is a bug in the SelectionBox class, or I miss something about it its configuration.

This is the code:

        protected override Control Render(string id, bool enabled, string value, bool hasError, List<Dynamicweb.UI.Elements.Actions.FieldAddOn> addOns)
        {
            SelectionBox mSelectionBox = new SelectionBox();
            mSelectionBox.ID = id;
            mSelectionBox.ShowSortRight = true;

            ListItem[] mListItem;
            mListItem = CreateListItemArrayWithAllNames();
            mSelectionBox.InputLeft = mListItem;
            mListItem = CreateListItemArrayFromValue(value); // ...But the 'value' input is always empty, so an empty list is created!!!
            mSelectionBox.InputRight = mListItem; 
            return mSelectionBox;
      }

Any help will be apprecisted,
Tomas

 


Replies

 
Viktor Letavin Dynamicweb Employee
Viktor Letavin
Reply
This post has been marked as an answer

Hi Tomas,

For some reason the selectionbox control doesn't store it is value without additional javascript. I can suggest folowing workaround: return div container instead of selectionbox control itself. The container should have the selection box control, hidden for value, script block for onchange like this:

protected override Control Render(string id, bool enabled, string value, bool hasError, List<Dynamicweb.UI.Elements.Actions.FieldAddOn> addOns)
        {
            SelectionBox mSelectionBox = new SelectionBox();
            mSelectionBox.ID = id;
            mSelectionBox.ShowSortRight = true;
 
            ListItem[] mListItem;
            mListItem = new ListItem[] { new ListItem("uno","1"), new ListItem("dos", "2") };
            mSelectionBox.InputLeft = mListItem;
            mListItem = CreateListItemArrayFromValue(value);
            mSelectionBox.InputRight = mListItem;
            var container = new HtmlGenericControl("Div");
            container.Controls.Add(mSelectionBox);
            container.Controls.Add(new LiteralControl($"<input type=\"hidden\" id=\"{id}\" name=\"{id}\" value=\"{value}\">"));
            container.Controls.Add(new LiteralControl($"<script type=\"text/javascript\">window.{id}_onChange = function() {{document.getElementById('{id}').value=SelectionBox.getElementsRightAsArray('{id}');}};</script>"));
            return container;
        }

 

Best Regards, Viktor.

Votes for this answer: 1
 
Tomas Gomez
Reply

Thanks Viktor, your script works perfect!

For future releases, it would be nice to solve this issue for SelectionBox, i.e. to have a similar behaviour than the DropDownList example.

BR,
Tomas

 
Tomas Gomez
Reply

One last question about this code.

When using this ProductFieldTypeProvider to render a custom field in a Product group (via ProductCategoryFields as indicated here), is there any way to get the ID of the Product group under edition?

The aforementioned Render method doesn't provide any input for this. Maybe it is possible to get the current Product group through other methods.

BR,
Tomas

 
Viktor Letavin Dynamicweb Employee
Viktor Letavin
Reply

Hi,

Currently you cannot know what group is currently editing during render field. This sounds like a good adjustment, but I am not sure when it will be implemented, you can write on feature request thread, probably guys will consider it.

BR, VIktor.

 
Tomas Gomez
Reply

Thanks Viktor,

Yes, it would good to have this feature. I think a ProductFieldTypeProvider for ProductGroups, discussed here as a requested feature, will do it.

BR, Tomas

 

You must be logged in to post in the forum