Validating for Empty Listbox

  • Thread starter Thread starter Dave
  • Start date Start date
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?
 
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.
 
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.
 
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)
 
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.
 
Back
Top