IsNull Test ???

E

EDOnLine

New asp.net/webform user question......

I am trying to use the Custom Validator control to check to see if either of
two fields on my form have been filled in. At this point all I am
interested in is if the user has entered a value into EITHER input box (not
left blank). Below is my control code and also my function. I guess my
question is, What should I be testing for in my function? I have tried many
things (checked the length of the textbox, null, empty string, etc.) and
nothing seems to be working (it returns TRUE).

However, if I test to see if either of the textboxes has a particular value
("1" for example), it works just fine (returns FALSE). So my theroy now is
that I am not testing for null or a empty textbox correctly. What is the
correct test for this?


<asp:CustomValidator runat="server" id="custPhonePagerCheck"
ControlToValidate="txtPhone" OnServerValidate="PhoneNumberCheck"
ErrorMessage="Phone or Pager Number is Required" />

Sub PhoneNumberCheck(sender as Object, args as ServerValidateEventArgs)
If args.value = "" AND txtpager.text = "" Then
args.IsValid = False
Exit Sub
End If
args.IsValid = True
End Sub



Much thanks in advance for any help!

-- Eric
 
C

Chris Jackson

You should be able to check for control.text.length != 0 in order to see if
there is user input.
 
E

EDOnLine

Yeah, that's what I thought too; but it didn't work either. I've tested for
length (.length and length function) and "" and IsDBNull.

What's weird is that if I test for a particular value (the number 1, for
example) it works just fine.

It's probably some simple mistake that I'm making. I am fairly new at this
but it seems that it should work.

Any ideas?
 
M

mikeb

EDOnLine said:
New asp.net/webform user question......

I am trying to use the Custom Validator control to check to see if either of
two fields on my form have been filled in. At this point all I am
interested in is if the user has entered a value into EITHER input box (not
left blank). Below is my control code and also my function. I guess my
question is, What should I be testing for in my function? I have tried many
things (checked the length of the textbox, null, empty string, etc.) and
nothing seems to be working (it returns TRUE).

However, if I test to see if either of the textboxes has a particular value
("1" for example), it works just fine (returns FALSE). So my theroy now is
that I am not testing for null or a empty textbox correctly. What is the
correct test for this?


<asp:CustomValidator runat="server" id="custPhonePagerCheck"
ControlToValidate="txtPhone" OnServerValidate="PhoneNumberCheck"
ErrorMessage="Phone or Pager Number is Required" />

Sub PhoneNumberCheck(sender as Object, args as ServerValidateEventArgs)
If args.value = "" AND txtpager.text = "" Then
args.IsValid = False
Exit Sub
End If
args.IsValid = True
End Sub

The CustomValidator validation handler doesn't get called if the control
it's validating (txtPhone in your example) is empty. So, I suspect that
what you're seeing is not that your validation handler is returning True
when both textboxes are empty, but that your validation handler is not
being called at all. This is a behavior that catches a lot of people
the first time they try using a custom validator.

What you want to do is *not* specify a ControlToValidate in your
CustomValidator control (CustomValidators are the only validation
control where a ControlToValidate does not need to be specified).

Then, change your handler slightly to have this line for the test:

If txtPhone.text = "" And txtPager.Text = "" Then

since the args parameter will not have any useful information (because
there's no ControlToValidate).

Now your CustomValidator's OnServerValidate handler will be called every
time the form is posted back to the server.
 
E

EDOnLine

Thanks for pointing that out!

So are you saying that the args paramater is useless now? If so, how am I
suppose to tell the validator that it failed the test?

I'm asking that because now, no matter what I put in either textbox, I
always get a FALSE. And after the post back the values that I put in the
boxes are no longer there. Weird. Below is the current version of my
code....

Thanks in advance for any pointers! (Man, I'm going to go nuts before I
learn all of this!)


<asp:CustomValidator runat="server" id="custPhonePagerCheck"
OnServerValidate="PhoneNumberCheck" ErrorMessage="Phone or Pager Number is
Required" />


Sub PhoneNumberCheck(sender as Object, args as ServerValidateEventArgs)

If txtPager.text = "" And txtPhone.text = "" Then
args.IsValid = False
Exit Sub
End If

args.IsValid = True

End Sub
 
M

mikeb

EDOnLine said:
Thanks for pointing that out!

So are you saying that the args paramater is useless now? If so, how am I
suppose to tell the validator that it failed the test?

I'm asking that because now, no matter what I put in either textbox, I
always get a FALSE. And after the post back the values that I put in the
boxes are no longer there. Weird. Below is the current version of my
code....

Thanks in advance for any pointers! (Man, I'm going to go nuts before I
learn all of this!)


<asp:CustomValidator runat="server" id="custPhonePagerCheck"
OnServerValidate="PhoneNumberCheck" ErrorMessage="Phone or Pager Number is
Required" />


Sub PhoneNumberCheck(sender as Object, args as ServerValidateEventArgs)

If txtPager.text = "" And txtPhone.text = "" Then
args.IsValid = False
Exit Sub
End If

args.IsValid = True

End Sub

This validation handler works for me.

I think you're going to have to post more code - try to cut it down to
the smallest possible ASPX page that reproduces the problem.

What tools are you using? if you're using VS.NET then debugging this
should be pretty easy. If you're not using VS.NET, then using the
Framework SDK's CLR Debugger (dbgclr.exe) is almost as good. Note that
dbgclr.exe is not in the path by default - it's in the GuiDebug folder
of the Framework's directory.
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top