DropDownList, ListItems and SelectedItem - asp.net

See also: Dropdowns filled with same list item
After a day of tracing and debugging, I finally figured out that populating 3 DropDownLists with the same collection of items causes the last selected item to appear in all 3 lists.
This appears to be due to the 'selectedness' of an item being a property of the item, rather than a property of the list.
However, this appears to be the case only when an item is selected programmatically.
edit #2: As everyone seems to be answering the wrong question: The following is what is confusing me!
The application appears to work as intended when the user selects items via the control. -- Selecting 3 different items in the Web interface results in the correct 3 different items being entered in the DB.
Can anyone explain why this is the case?
EDIT: The question I am asking is why does it work at all in the browser?

It's because you're adding the same ListItem to two different DropDownList controls. The object that is being displayed in each of them is the same so changing the value of that object will be reflected in both DropDownLists.
Think of the dropdowns as being just a way of looking at a collection of objects. You poking the dropdown and telling it to change it's selected value really just results in it looping through the items in it's collection and changing their select value. If you use the same collection of objects for both drop downs, changing the values in the collection will result in both drop downs seeing the same change.
It's like having two windows in a house that both look out on the same dog house. If you were to tell a person looking out of window A to go and paint the dog house blue, even though you didn't tell the person looking out window B to paint "his" dog house, he's still going to see a blue dog house.

This has to do with BindingContext and the collection. You need to make copies of the collection and bind to those. I think this was done to make mother-child views easier, where you can get the correct child element, based on the selected mother element.
If your collection is a DataTable, binding a dropdownlist to it will use it's DefaultView. To avoid this, package the table in dataviews;
obj.DataSource = new DataView(dataTable, "", "", DataViewRowState.CurrentRows);

Related

How to figure out the index of a ToggleGroup's selected toggle?

I'm trying to select an item from a list that's sorted the same way as a ToggleGroup I have besides it. However, I found that toggleGroup.getToggles().indexOf(toggleGroup.getSelectedToggle()) always returns -1 (visible in the IndexOutOfBoundsException thrown as I pass it). Is there another way of figuring out the index, or am I at a loss with my approach and need to figure out something completely different?
UPDATE: Apparently, for the first time an item is selected (I have this code attached to changes of selectedToggleProperty()), it works fine (I just get no notice of it because the elements I make visible have no proper layout). However, when an item is selected while another item already is selected, getselectedToggle() becomes null, causing aforementioned behavior.
All of the JavaFX toggle controls have a property called UserData. You should use that to create the links between the toggles and data list. Relying on the index of the toggles in the toggle group is probably a bad idea.

ASP.net: Complex validation of dynamically created DropDownLists

I have a GridView with records of the database.
I create two DropDown lists dynamically for each selected item. That means, if I click select, two DropDown lists appear, the first one defines a start position, the second one a stop position. Both includes numbers (depending on how many items I have selected). If I have selected 5 items, all DropBox controls include the values 1 - 5. Everything is done by code behind.
Now I want to validate that. The stop Control shall be set to a higher one as the start control. Further more, each area that is already used, is not allowed for the next controls. E. g. if I select in the first pair of DropDown lists the area 1-5, I shall not be able to choose 2-6 with the next pair of controls.
How can I do that. I thought about javascript but that could be very tricky because everything is done with code behind. The validation controls are nice, but I don't know how to validate such a complex thing...
Any Ideas?
thank you for your answers!
I dont wont to do a async postback each time, especially because the lists shouldnt be limited during the changes. I solved it in that way:
The user clicks on save, I create a bool array for each dropdown item, then I loop through all dropdownlists in the placeholder. If there is a area from 3 - 5 I set the values with index 3-5 in my bool array to true. If one of these values is already true, there must be a wrong selection in the start/stop positions.
I think, its not a very nice way but works pretty fast and does everything I want. If someone has a better idea, please let me know... ;)
Thanks!
Stefan

What's the best practice for generating spark radio buttons?

I have a list of radio buttons, which I want to be able to both set the value of programmatically and for the user to set the value of manually.
I have a small list of data items which I want to display as options in a list of radio buttons. The objects are stored in a model object, as is the currently selected item. The currently selected item is bound to the radiobutton group. The radio buttons are generated using a spark list.
I am having a problem setting the list programatically - whenever I set the current value on the radiobutton group to the first value in the list, all of the radio buttons are cleared, where the first one should be selected. I when checking in the debugger, I found the likely reason - there are two radiobuttons in the group that point to the same value, one of which isn't showing. My best guess is that the list control has created an extra item renderer which it is holding on to in case it needs to scroll the list.
Is there a way to create radioButtons based on an ArrayCollection without using a list? Failing that, is it possible to prevent the list from generating the extra item?
Use a DataGroup with a dataProvider (an ArrayList of objects holding data) with a custom item renderer that creates the radiobutton that you need. Add proper bindings of the data object to the radiobutton (maybe even do 2 way binding for quick saving).
In the end, I gave spark best practices the finger and used a repeater, which created the correct number of radioButtons with no extras. It may be slow, but slow iteration over a set of less than ten items is O(I don't care).

ASP:ListBox | Multi Select | Keep selected values when selecting a new one

1) Have a listbox with 3 values out of 5 selected
2) When I click to select another value without holding CTRL button, it will unselect over values
How to make it keep other selected values if new value is selected?
This is going to sound like a snide answer, but I don't mean it that way. I just like to look for the simple solutions rather than the complicated onces.
The easiest way to get a control to have the behavior you want is to use a control that has the behavior that you want, rather than modifying the behavior of an existing control.
That said, if you want a list of items where a user can select a bunch of items off the list, and don't want to have to rely on them holding control, you're using the wrong tool for the job.
Use a CheckBoxList instead of a ListBox. If you want it to be scrollable, then set it in a div of a specific height, and set the style of the div to "overflow: scroll".
If you still want to use a ListBox you should use javascript and for each click event fired, you should check if the clicked element is selected/unselected and act accordingly. It's a little bit tricky but at least it is a solution for your problem.

Webforms App Layout Opinions?

I have a very simple webforms app that will allow field techs to order parts from the warehouse.
Essentially it work like so:
User selects a category from a filter dropdown, which then binds items of that category to a gridview control
User finds an item in the gridview and inputs a desired quantity (in a text box in a template field in each row)
User repeats 1 & 2 as needed
User sees a summary of the complete requisition
User confirms items and submits the requisition for processing
My no-brainer UI design so far is the generic dropdown-above-a-gridview where there's a category drop-down list that filters a gridview, like in the eye-catching asp.net ado tutorials:
    http://static.asp.net/asp.net/images/dataaccess/15fig01vb.png
Each gridview row (in my app, not in the image above) lists an item's details and can accept a quantity input in the template textbox if the user wants to requisition that item.
Given that a user will want items from different categories during a single usage session, I'm trying to figure out a good, user-friendly way to allow users to input a quantity for an item, have a warm fuzzy feeling that their input has been accepted/stored, then change the category filter (thus binding the gridview to a different set of data) and select other items from the gridview as many times as necessary before summing up their order and submitting it.
I've thought about putting another grid below the first and adding items to it dynamically as the user selects each item. But that seems awkward. Ditto with an unordered list or similar simple structure under the grid.
I've also thought about just storing the user's picks in view state or session state and displaying a summary on another page, kind of like a shopping cart sort of functionality. Maybe do that and use some sort of ajaxy goodness on the main page to display something warm and fuzzy when a quantity is input?
WWYD? What Would You Do?
TIA.
I strongly agree with your first choice: users need to see somewhere what they have chosen or they will probably keep choosing it over again thinking it failed. Waiting to display it on a summary page shouldn't even be an option. I don't see much wrong with binding to another grid, although a repeater is also a decent option. Well, there are many options. Anyway, if there is room to do this off to one side or another - especially the left- I definitely would, rather than at the bottom. Also, bonus points for enabling users to change the quantities (or delete all) of an item they already selected, wherever you choose to display this.
I like the idea of a search, but be careful with auto-complete. Google style is good where it displays results below, but I've seen people develop some that are way too aggressive and love to write over what you're typing: this is awful. Good luck.
Probably because both your choices are fine - it comes down to personal preference. The shopping cart idea is well known. But sometimes it gets old if you have to keep going back and forth between the cart and the item selection.
What's wrong with the separate grid? -That way you keep the selection list separate from the ordered items list?
Why tie the user to selecting the correct category and then selecting the quantity and hitting a button?
Why not use some type of autocomplete search so they can type in the produce name they want? Then a user could type "widg", get a drop town of choices, hit tab to go to a quantity field, enter a number, and then hit enter.
Display a quickie preview with the aucocomplete too, like as single row of your data.
Then throw all these into another grid at the top of the page if its a critical part of the application, maybe at the bottom if you think the actual grid results are important.
The reason I don't like category drop downs is people who are familiar with their jobs or company usually know the names and even skus numebers for what they are trying to do. Having them select a category instead of typing just slows them down. Also I hate running into the "which category is this?" moment. For example, is a chicken a pet, food, livestock, or food producer?

Resources