Groups | Blog | Home
all groups > asp.net webcontrols > march 2007 >

asp.net webcontrols : Using Javascript and Validation controls


Peter Afonin
3/13/2007 11:29:52 PM
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

marss
3/16/2007 7:25:37 AM

[quoted text, click to view]

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.
marss
3/16/2007 7:28:41 AM

[quoted text, click to view]

I mean
if (!confirm('... to submit change?')) return false;
Peter Afonin
3/16/2007 12:24:57 PM
Thank you very much, you were correct! Without return everything
works.

Peter

[quoted text, click to view]

Peter Afonin
3/19/2007 2:27:53 PM
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

[quoted text, click to view]

marss
3/20/2007 4:24:52 AM

[quoted text, click to view]

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

[quoted text, click to view]

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;"
Peter Afonin
3/20/2007 9:36:59 AM
Thank you, I'll try this.

Peter

[quoted text, click to view]

AddThis Social Bookmark Button