ASP.net: Complex validation of dynamically created DropDownLists - asp.net

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

Related

.NET dropdownlists with huge number of items

I am working on a webpart which makes use of 5 dropdowns (each dropdown represents 'Project' table columns like projectNo, ProjectPM, ContractNo etc). The table has more than 3000 items, so when the dropdownlists are finally databound, they have more than 3000 items to load, which pretty much brings IE to a halt.
What is the best way to go about architecting a solution, so users can still make use of the dropdown list interface, may be like first rendering an empty dropdownlist and then using a modal window to first find the project number they are looking for and then set the item as selected in the dropdownlist. I am envisioning the following UI. Can anyone guide me on how I should go about solving this issue?
-------------------
|V| [SearchBtn]
-------------------
-------------------
|V| [SearchBtn]
-------------------
-------------------
|V| [SearchBtn]
-------------------
[FindProjectBtn]
Clickikng on the searchBtn basically brings up a popup window where they can search for their stuff, and when they click on the matched result, it will set the dropdownlist.
When working with dropdown lists, a good rule-of-thumb is to keep the list short so you do not experience browser performance issues. If you have thousands of items to look through you can:
Create a search component whereby the user enters a part of the project name they are searching for and display a simple grid for them to choose from.
Implement a type-ahead mechanism. The user enters the first couple of letters of what they are searching for. You can query your DB for the top 10 hits and return them via AJAX.
If you have 3000 items, then definitely drop down list will not be a right designing decision.
using any kind of repeater lists with paging, sorting and searching functions is highly recommended in your case.
but if you insist to have a kind of drop down list view then Matthew's suggestions are your answer.

DataRepeater becomes empty

I'm using a DataRepeater control in my VB.Net application so I can make a custom-made list control.
I know it's main functionality is to work with dataSets and stuff, but I don't actually want to transfer my stuff into a database because in the program's context it doesn't actually make any sense.
So my goal here is to use a dataRepeater to list the contents of a mail inbox.
Everything works just fine until I scroll down the control. As I scroll down/up to see every item (in this case, every listed e-mail), the contents disappear and all I can see is a bunch of lines with the controls' default values assigned - like "Label1" and so on.
This is how I'm filling the dataRepeater (abreviated):
For each m as Message in mail.Inbox
dr.Addnew()
With dr.CurrentItem
.Controls("lblFrom") = m.From
.Controls("lblSubject") = m.Subject
End With
Next
As I said, the dataRepeater shows up perfect until I scroll it up/down, and when I do it all the data just disappears remaining only the items with the controls' default (design-time) values.
Am I missing something here? Or is it just not possible to work with a dataRepeater to fulfil my idea?
UPDATE:
Got my answer right here!
The repeater isn't going to hold the values on its own. You dont have to use a datasource, but you have to use something. Here is an example where they used a simple integer array: VB.NET Repeater Simple Data Binding Without Datasource

DropDownList, ListItems and SelectedItem

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

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