Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Using Javascript and Validation controls

Reply
Thread Tools

Using Javascript and Validation controls

 
 
Peter Afonin
Guest
Posts: n/a
 
      03-14-2007
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


 
Reply With Quote
 
 
 
 
marss
Guest
Posts: n/a
 
      03-16-2007

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.

 
Reply With Quote
 
 
 
 
marss
Guest
Posts: n/a
 
      03-16-2007

marss wrote:

> if (confirm('... to submit change?')) return false;


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

 
Reply With Quote
 
Peter Afonin
Guest
Posts: n/a
 
      03-16-2007
Thank you very much, you were correct! Without return everything
works.

Peter

On Mar 16, 7:25 am, "marss" <(E-Mail Removed)> wrote:
> 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.



 
Reply With Quote
 
Peter Afonin
Guest
Posts: n/a
 
      03-19-2007
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" <(E-Mail Removed)> wrote:
> 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.



 
Reply With Quote
 
marss
Guest
Posts: n/a
 
      03-20-2007

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;"

 
Reply With Quote
 
Peter Afonin
Guest
Posts: n/a
 
      03-20-2007
Thank you, I'll try this.

Peter

On Mar 20, 4:24 am, "marss" <(E-Mail Removed)> wrote:
> 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;"



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A form validation library with javascript validation Grigory Temchenko Python 0 12-28-2008 08:38 PM
Using Javascript and Validation controls Peter Afonin ASP .Net Web Controls 6 03-20-2007 04:36 PM
where is the validation occurs for validation controls baroque Chou ASP .Net 4 01-24-2006 03:32 PM
Page appears to fail validation even though it has no validation controls =?Utf-8?B?ZGh1cndpdHo=?= ASP .Net 1 04-10-2004 07:37 AM
ASP.NET Web Forms Validation Controls are Server-Side or Client-Side Validation? Matt ASP .Net 14 01-30-2004 09:15 AM



Advertisments