By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

Using Javascript and Validation controls

P: n/a
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,

--
Peter Afonin
Mar 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Peter Afonin wrote:
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.

Mar 16 '07 #2

P: n/a

marss wrote:
if (confirm('... to submit change?')) return false;
I mean
if (!confirm('... to submit change?')) return false;

Mar 16 '07 #3

P: n/a
Thank you very much, you were correct! Without return everything
works.

Peter

On Mar 16, 7:25 am, "marss" <marss...@gmail.comwrote:
PeterAfoninwrote:
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.

Mar 16 '07 #4

P: n/a
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

On Mar 16, 7:25 am, "marss" <marss...@gmail.comwrote:
PeterAfoninwrote:
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.

Mar 19 '07 #5

P: n/a

Peter Afonin wrote:
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;"

Mar 20 '07 #6

P: n/a
Thank you, I'll try this.

Peter

On Mar 20, 4:24 am, "marss" <marss...@gmail.comwrote:
PeterAfoninwrote:
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;"

Mar 20 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.