Validating for Empty Listbox

D

Dave

I recently found out that the RequiredFieldValidator works differently
for a ListBox than I needed; it checks to see if a row is selected,
rather than checking to see if the listbox is empty. I tried using a
CustomValidator but it does not seem to be firing (I have e.false in
the validation function but still no intervention). Does the
CustomValidator also require that a row is selected? Can I use
validators for this requirement or do I have to find some other way?
 
R

Riki

Dave said:
I recently found out that the RequiredFieldValidator works differently
for a ListBox than I needed; it checks to see if a row is selected,
rather than checking to see if the listbox is empty. I tried using a
CustomValidator but it does not seem to be firing (I have e.false in
the validation function but still no intervention). Does the
CustomValidator also require that a row is selected? Can I use
validators for this requirement or do I have to find some other way?

Maybe I'm missing something here, but the number of items in a listbox is
not something the user can change (unless you have written extra code for
it).
Therefore, it's not supposed to be validated.

The CustomValidator does not know anything about selected rows.
In fact it doesn't know anything about your controls. You don't even have to
set the ControlToValidate property.
You are supposed to write code for the custom validator, and in that code
you can validate what you want.
 
D

Dave

Maybe I'm missing something here, but the number of items in a listbox is
not something the user can change (unless you have written extra code for
it).
Therefore, it's not supposed to be validated.

The CustomValidator does not know anything about selected rows.
In fact it doesn't know anything about your controls. You don't even have to
set the ControlToValidate property.
You are supposed to write code for the custom validator, and in that code
you can validate what you want.

I must be doing something messed up then. It does not seem to want to
fire...

HTML:

<asp:ListBox ID="FB" Width="95%" runat="server" font-
size="Small" rows="5"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1"
runat="server"
Display="Dynamic"
OnServerValidate="Validate_Files"
Text="* Please upload files."
runat="server"/>

ASP FUNCTION:

private void Validate_Files(object sender, ServerValidateEventArgs
e)
{
e.IsValid = false; // Just Testing!
}


But I do not get the error message like I expected. I have other
RequiredFieldValidator's on the page that work. On a side note, there
is also the issue of wierd behavior -- after a
window.location.replace(...) or refresh the validation function does
fire for some reason I don't understand (the only time it does). But
the page can still be submitted no matter what.
 
B

bruce barker

you only supplied server code, so the postback will happen. you must
take some action, say check isvalid, and not perform any actions to see
the message.

-- bruce (sqlwork.com)
 
D

Dave

I must be doing something messed up then. It does not seem to want to
fire...

HTML:

<asp:ListBox ID="FB" Width="95%" runat="server" font-
size="Small" rows="5"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1"
runat="server"
Display="Dynamic"
OnServerValidate="Validate_Files"
Text="* Please upload files."
runat="server"/>

ASP FUNCTION:

private void Validate_Files(object sender, ServerValidateEventArgs
e)
{
e.IsValid = false; // Just Testing!
}

But I do not get the error message like I expected. I have other
RequiredFieldValidator's on the page that work. On a side note, there
is also the issue of wierd behavior -- after a
window.location.replace(...) or refresh the validation function does
fire for some reason I don't understand (the only time it does). But
the page can still be submitted no matter what.- Hide quoted text -

- Show quoted text -

I figured it out. Needed to use ClientValidationFunction instead of
ServerValidationFunction. Sorry about posting the bad code.

Thanks.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top