How Do I Combine the Cleint & Server Script?

P

Prodip Saha

Dear ASP.NET Gurus,
I have a TextBox control with AutoPostBack set to true to execute the server
scripts. I also, added some client script for validation.What I want
is--execute the client script and if the cleint script returns true only
then I want to execute the server scripts. Am I asking too much or is it
achievable? Please don't give me an answer like --just validate on the
server side...

Some Background:-
I have deligated an event handler to the textChanged event on the TextBox
control and I have added the client script as an attribute of the TextBox. I
have tried the code different ways but with no luck...

txtTest.Attributes.Add("onChange","IsCurrency('txtTest');");
txtTest.Attributes.Add("onChange","if(IsCurrency('txtTest')){doSomething()}e
lse ");

The first approach will add __doPostBack script automatically at the end of
the client script (after the semi colon) since I have the auto postback set
to true. Problem is --it execute both the client and server scripts.

The second approach will add __doPostback script after the "else" keyword.
This sounds a doable one. The problem is--it does not execute the server
script (i.e. the __doPostback does not get executed to submit the Form).

Thanks in advance.
Prodip Saha
 
G

Guest

Try this, change the following
txtTest.Attributes.Add("onChange","IsCurrency('txtTest');")
t
txtTest.Attributes.Add("onChange","return IsCurrency('txtTest');")

Remove your _dopostback call from the IsCurrency function. Keep Autopostback of the textbox as true
Inside the IsCurrency return true if the validation passes or false if it fails

See if that helps
Suresh


----- Prodip Saha wrote: ----

Dear ASP.NET Gurus
I have a TextBox control with AutoPostBack set to true to execute the serve
scripts. I also, added some client script for validation.What I wan
is--execute the client script and if the cleint script returns true onl
then I want to execute the server scripts. Am I asking too much or is i
achievable? Please don't give me an answer like --just validate on th
server side..

Some Background:
I have deligated an event handler to the textChanged event on the TextBo
control and I have added the client script as an attribute of the TextBox.
have tried the code different ways but with no luck..

txtTest.Attributes.Add("onChange","IsCurrency('txtTest');")
txtTest.Attributes.Add("onChange","if(IsCurrency('txtTest')){doSomething()}
lse ")

The first approach will add __doPostBack script automatically at the end o
the client script (after the semi colon) since I have the auto postback se
to true. Problem is --it execute both the client and server scripts

The second approach will add __doPostback script after the "else" keyword
This sounds a doable one. The problem is--it does not execute the serve
script (i.e. the __doPostback does not get executed to submit the Form)

Thanks in advance
Prodip Sah
 
P

Peter Blum

When AutoPostBack is enabled, it adds client-side scripts to the control
that call __doPostBack without calling the client-side validation function.
Here's how to fix that:
1. Set AutoPostBack to false. (Yes, turn it off. We will implement it
again.)
2. Add an onchange event that calls the scripts returned by
Page.GetPostBackEventReference().
TextBox1.Attributes.Add("onchange", Page.GetPostBackEventReference(TextBox1,
""))

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 
P

Prodip Saha

Hi Peter,
Thanks for the reply. I have tried the approach that you have mentioned. The
code looks as if it will submit the form but it does not submit unless I
have the AutoPostBack set to True. The html (in view source) look like the
following --

<td>
<input
name="txtTest"
type="text"
value="0.00"
id="txtTest"
onChange="if(ValidateControlForCurrency('txtTest')){
__doPostBack('txtTest',''); }"
language="javascript"
style="width:100px;"
/>
</td>

Any creative idea is welcome. Thanks.
Prodip
 
P

Peter Blum

My answer was designed around having the ASP.NET validators run on the page
in the onchange event. You show code that tries to validate some other way.
I'm not sure why. The CompareValidator can validate currency. Set its
Type=Currency and Operator=DataTypeCheck.

I did make a mistake with my previous answer. I thought that
Page.GetPostBackEventReference() created the validation javascript code as
well as __doPostBack. It only creates __doPostBack.

1. Use the CompareValidator
2. TextBox1.Attributes.Add("onchange", "if (typeof(Page_ClientValidate) ==
'function') {Page_ClientValidate(); if (!Page_BlockSubmit)" +
Page.GetPostBackEventReference(TextBox1, "")) + "}"

If you try it with your custom function, do this:
TextBox1.Attributes.Add("onchange", "if
ValidateControlForCurrency('txtTest')){" +
Page.GetPostBackEventReference(TextBox1, "")) + "}"

Be aware that AutoPostBack writes its own onchange code. You will see 2
onchange attributes when its on. The first of them is run by the browser.
That usually is the AutoPostBack code, not yours.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top