Using Javascript and Validation controls

P

Peter Afonin

Hello,

I'm creating an application in ASP.NET 1.1. I need to check whether at least
one checkbox in my datagrid has been checked. To do this, I'm using
Javascript - I'm adding this code to Page_Load event:

Dim iCount As Int32
Dim sClientSideValidate As New StringBuilder
iCount = dgReport.Items.Count
For i As Int32 = 2 To iCount + 1
If Not i = iCount + 1 Then
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked ||")
Else
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked")
End If
Next
btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
& ")){ return confirm('You have not selected any DRs. Are you sure you want
to submit change?');}")
'RELEASE RESOURCES
sClientSideValidate = Nothing

This works great. The problem is that if I use this script, my other ASP.NET
validation controls on this page no longer work. I can see why - since the
Javascript runs on button onclick (client) event, I guess all other
Javascript on this page is ignored.

I wonder - is there a way to use both - javascript validation and ASP.NET
1.1 validation contols on the same page, or I have to put all validation in
JS?

I would appreciate your help.

Thank you,
 
M

marss

Peter said:
btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
& ")){ return confirm('You have not selected any DRs. Are you sure you want
to submit change?');}")
'RELEASE RESOURCES
sClientSideValidate = Nothing

This works great. The problem is that if I use this script, my other ASP.NET
validation controls on this page no longer work. I can see why - since the
Javascript runs on button onclick (client) event, I guess all other
Javascript on this page is ignored.

I wonder - is there a way to use both - javascript validation and ASP.NET
1.1 validation contols on the same page, or I have to put all validation in
JS?

Hi,
I think your "return confirm(..." breaks off the client-side
validation of ASP validation controls.

Try to replace
return confirm('... to submit change?');

with
if (confirm('... to submit change?')) return false;
Maybe it helps.
 
P

Peter Afonin

I'm still having a problem here, because I do need to use 'return
false' at the end:

btnSubmit.Attributes.Add("onClick", "if (!(" &
sClientSideValidate.ToString
& ")){ return confirm('You have not selected any DRs. Are you sure you
want
to submit change?');return false;}")

If I use it - my other validation controls are dead.

I wonder - are there any substitutes for 'return false'?

Thank you,

Peter
 
M

marss

Peter said:
I'm still having a problem here, because I do need to use 'return
false' at the end:

Why? You should to use 'return' only if "your condition"=false
btnSubmit.Attributes.Add("onClick", "if (!(" &
sClientSideValidate.ToString
& ")){ return confirm('You have not selected any DRs. Are you sure you
want
to submit change?');return false;}")

If I use it - my other validation controls are dead.

I wonder - are there any substitutes for 'return false'?

Thank you,

Peter

Run your project and look through the page source.
You can find that the Button control is rendered as a html like this:

<input type="submit" name="btnSubmit" value="Submit" onclick="if
(typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
language="javascript" id="btnSubmit" />

Here "if (typeof(Page_ClientValidate) == 'fu..." is the script to call
the client-side validation. If you add one more event handler for
client-side "onclick" event it will be added right before above
script.
Example:

btnSubmit.Attributes.Add("onclick", "return false;");

causes next html to be rendered:
.... onclick="return false;if (typeof(Page_ClientValidate) ==
'functio...

The "return" breaks off the next code execution. So you should write
your script in the way that does not interrupt the validation if
everything is ok.
Try something like this:

"if (!(" & sClientSideValidate.ToString & ") || confirm('...?'))
return false;"
 
P

Peter Afonin

Thank you, I'll try this.

Peter

PeterAfoninwrote:

Why? You should to use 'return' only if "your condition"=false








Run your project and look through the page source.
You can find that the Button control is rendered as a html like this:

<input type="submit" name="btnSubmit" value="Submit" onclick="if
(typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
language="javascript" id="btnSubmit" />

Here "if (typeof(Page_ClientValidate) == 'fu..." is the script to call
the client-side validation. If you add one more event handler for
client-side "onclick" event it will be added right before above
script.
Example:

btnSubmit.Attributes.Add("onclick", "return false;");

causes next html to be rendered:
... onclick="return false;if (typeof(Page_ClientValidate) ==
'functio...

The "return" breaks off the next code execution. So you should write
your script in the way that does not interrupt the validation if
everything is ok.
Try something like this:

"if (!(" & sClientSideValidate.ToString & ") || confirm('...?'))
return false;"
 

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

Top