Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

custom validator not working? 1

Status
Not open for further replies.

Junior1544

Technical User
Apr 20, 2001
1,267
US
I am tring to make a server side custom validator...

It wont fire at all... I'm using vs.net.

Here is the html for the control:
<asp:customvalidator id=&quot;customvalidator1&quot; runat=&quot;server&quot; controltovalidate=&quot;name&quot; errormessage=&quot;Invalid Name.&quot;>*</asp:customvalidator>

and in the vb codebehind file this is the code for it...
(keeping in mind, i'm just tring to get it to fire...)

Private Sub Custom Validator1_ServerValidate(byval source as System.object, byval args as System.web.ui.webcontrols.servervalidateeventargs) handles customvalidator1.servervalidate

args.isvalid=false

end sub

Any idea's??

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
It looks like your problem is that there isn't a required property set that wires the custom server-side validation routine to your CustomValidator server control on your page. You'll need to use this:

<asp:customvalidator id=&quot;customvalidator1&quot; runat=&quot;server&quot; controltovalidate=&quot;name&quot; errormessage=&quot;Invalid Name.&quot; OnServerValidate=&quot;Validator1_ServerValidate&quot;>*</asp:customvalidator>

I wrote a column on how to use CustomValidator controls at:
Good luck!

---------------------------------------------------
Jason Salas, MBA, MCP
Web Development Manager
Pacific Telestations, Inc. (dba, &quot;KUAM&quot;)
URL: President, .NET User Group of Guam
URL: Mailto: jason@kuam.com
 
When I put that in It gave me a compilation Error...

Description: An error occurred during th comilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Comiler Error Message: BC30390: 'Localhost.frmSignIn.Private Sub CustomValidator1_serverValidate(source as Object, args As System.web.ui.webcontrols.servervalidateeventargs)' is not accessible in this context because it is 'Private'.

Any idea's??

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
You need to change the accessor level of the method to &quot;Public&quot;. Replace the code in the code-behidn file with this:

Public Sub Custom Validator1_ServerValidate(byval source as System.object, byval args as System.web.ui.webcontrols.servervalidateeventargs) handles customvalidator1.servervalidate

Note the use of the reserved word &quot;Public&quot; in the method event definition. You could also use &quot;Protected&quot; and it should still work.


Jason
 
ok, this is what I have in the Code Behind... And it doesn't seem to do any thing... am i missing some thing??

--James

(on a side note, the page does load without an error now:))

Public Sub CostomValidator1_ServerValidate(ByVal source as System.Object, ByVal args as System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
args.IsValid = False
End Sub

junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Oh, and for the help I gave you a star... I wish i could do more:)

Sooner or later I'll be able to start giving answers back to people:)

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
That's the tricky part...you have to wire it to some custom server-side method that you need to wirte to get the custom validation you're after. What exactly are you tryig to validate against?

Jason

p.s. thanks for the star. my first! :)
 
The end result will be to check to see if the value in a text box (user entered) is contained in a table. If the value is not in the table, reject it and throw the error... If it is in the table it can save the data...

What i'm tring to get it to do it throw up the error reguardless... once i'm able to do that, then i know it's &quot;Wired up&quot; and can then make the code how i need it...

I'm ok with VB and know how i need the check writen, i just need the event to fire...

Thanks...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
All you need to do is write a custom event to determine the validity of your operation/data (which is what you've got already). You would then set this boolean value to the e.IsValid property for the ServerValidateEventArgs argument in your event handler.

You'll probably need to wire a seperate method to a Button, LinkButton or ImageButton control and then evaluate the Page.IsValid property to determine what to do. Check out how I did this in the code at:
Jason
 
that's a very good article you wrote...

My main problem at this moment, acording to that and acording to help files i've read and every thing else... My custom validator should be working... but it's not.

I'm realy confused...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
ok, Now i'm even more confused... I was just tring a couple of things and found that the server is running the event... but doesn't do any thing when i set args.isvalid = false

it still goes right ahead and tries to save the data entered into the database...

i'm about to loose it on this project;-)

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Ok, I got some thing working... I'm not happy about it, but i guess it's got to do...

I have a save button on the bottom and i'm doing the error checking in there...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Can you post the URL of the oage you're trying to work on...or the entire code block? This might help. :)
 
I'm developing soly on a laptop... the computer i'm developing on isn't even connected to a network or any thing... (it's annoying, i have to type every thing from the one pc to this one...)

I'll see what i can do...

--James

Here's basicly what i've done...

I used the custom validators... They ran and every thing, but wouldn't stop the page from posting and saving the data to the database. so uppon your suggestion earlier i did this:
Code Snipit to follow
----------------

if Page.IsValid = true then
'here would be the code to save the data to the database.
else
'ahh, there is an error from a custom validator. i'll
'throw an error and reload the page.

dim strScript as string = &quot;&quot;
if customvalidator1.isvalid = false then
strScript = &quot;This is the error for Customvalidator1.\n&quot;
end if
if customvalidator2.isvalid = false then
strScript &= &quot;This is the error for Customvalidator2.\n&quot;
end if
' The &quot;\n&quot; in the string gives a line feed in the error
' box to come up.
strScript = &quot;<script language=javascript> alert(&quot;&quot;&quot; & strScript & &quot;&quot;&quot;)</script>&quot;
response.write(strScript)
response.flush()

end if
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top