Help with Multiple Select ListBox

S

sab

Hi,
We have a web form with a listbox. The listbox is a multiple select listbox
and has data that looks something like:

ALL
Unit 1
Unit 2
Unit 3

Note: "ALL" is always the first item in the list.

Anyway, ALL can be selected, but only if it is the only item selected (i.e.,
an invalid selection would be to select ALL and Unit 1). The other "Unit"
items can be selected by themselves or with multiple "Unit" items (e.g.,
Unit 1, or Unit 2 and Unit 3, etc.). The validations are:

If ALL and no Unit items selected, then valid
If ALL and one or more Unit items selected, then invalid
If one or more Unit items selected (without ALL being selected), then valid

We're having trouble figuring out how to validate that the user selected a
valid set of items.

Can anyone be of assistance? How do we write the code to validate this
situation? In what event should the code be placed? Etc?

Thanks in advance.
 
G

Guest

sab said:
Hi,
We have a web form with a listbox. The listbox is a multiple select listbox
and has data that looks something like:

ALL
Unit 1
Unit 2
Unit 3

Note: "ALL" is always the first item in the list.

Anyway, ALL can be selected, but only if it is the only item selected (i.e.,
an invalid selection would be to select ALL and Unit 1). The other "Unit"
items can be selected by themselves or with multiple "Unit" items (e.g.,
Unit 1, or Unit 2 and Unit 3, etc.). The validations are:

If ALL and no Unit items selected, then valid
If ALL and one or more Unit items selected, then invalid
If one or more Unit items selected (without ALL being selected), then valid

We're having trouble figuring out how to validate that the user selected a
valid set of items.

Can anyone be of assistance? How do we write the code to validate this
situation? In what event should the code be placed? Etc?

Thanks in advance.

You may want to try to use a custom validator. It would be someting like:

....
<script>

function myClientValidationFunction(objSender, objArgs) {
// Write your implementation here using javascript

// If ALL and no Unit items selected, then valid
objArgs.IsValid = true;
// If ALL and one or more Unit items selected, then invalid
objArgs.IsValid = false;
// If one or more Unit items selected (without ALL being selected), then
valid
objArgs.IsValid = true;
};

</script>
....
<asp:listbox runat=server id=myListBox multiple=true />
....
<asp:customvalidator runat=server id="myCustomValidator"
clientvalidationfunction="myClientValidationFunction" ErrorMessage="My Error
Message">*</asp:customvalidator>
....

By doing this, you will be able to client side validate that the selected
items are valid. Do not forget to implement the
myCustomValidator.ServerValidate function and ask "If Page.IsValid" before
doing anything as well.

Should you have any questions or comments, let me know

Regards,
Cesar Saucedo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top