Using both server side validation and client side validation

G

Guest

Hi,

I have a button which is a web control. I have some validation in javascript
for the button in .aspx file and some in the button onclick event in code
behind(C#). I need to get through both the java script validation as well as
the click in code behind? How can this be done ? Can I call the code behind
from the javascript function?
If not can i have a separate procedure in C# file and call it from the
javascript function? If so what is the syntax for it?
Please let me know even if it's silly as I am pretty new to .net
 
K

Kevin Spencer

Hi vidya,

In HTML, there is a "last chance" event that fires when a form is submitted,
called "onsubmit". If you create a JavaScript "onsubmit" event handler for
the form, a function that returns true or false, you can have a client-side
validation script that fires before the server-side validation. Here's an
example:

<form name="MyForm" action="ThisUrl" onsubmit="return myValidation(this)">

Any time you wire up a server-side event handler for a Web Control, what
happens is this: A client-side JavaScript event handler for the client-side
HTML form element of that Web Control is added to its HTML. That client-side
event handler puts data about what element fired the event and any
additional event-related data, into a couple of hidden HTML form fields that
are also added to the HTML form. The client-side event handler then submits
the form via JavaScript. This fires the "onsubmit" event of the client-side
HTML form. When you add a JavaScript "onsubmit" event handler to the form,
you are intercepting that form post event, and if your JavaScript function
returns true, the form is submitted. If it returns false, the form submit is
aborted.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 

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,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top