Javascript not working

T

tshad

I have the following code that attaches a Javascript confirm box to my
checkbox. When I select the checkbox, the window comes up fine, but it
never executes the XfertoDefault_Click function when I press the Yes button.
If I take off the Javascript event, the checkbox works fine.

XferToDefault.Attributes.Add("onclick", "return confirm('Are you sure you
want copy these entries?');")

<asp:CheckBox ID="XferToDefault" AutoPostBack="true"
OnCheckedChanged="XferToDefault_Click" runat="server"/>

If I change the checkbox to an ImageButton, it all works fine - the
Javascript window as well as going to the function after the Yes button is
pressed.
<asp:ImageButton ID="XferToDefault" OnClick="XferToDefault_Click"
ImageUrl="../../buttons/xfer.gif" runat="server"/>

Why doesn't the checkbox (or radiobutton) work?

Thanks,

Tom
 
T

tshad

Eliyahu Goldin said:
Tom,

Try

XferToDefault.Attributes.Add("onclick", "if(!confirm('Are you sure you want
copy these entries?')return;")

I tried that, but there must be a problem with the Javascript. I don't get
the confirm box at all now, as well as not going to the function.

Tom
 
E

Eliyahu Goldin

Tom,

Try

XferToDefault.Attributes.Add("onclick", "if(!confirm('Are you sure you want
copy these entries?')return;")

If it works, I can explain the reason.

Eliyahu
 
E

Eliyahu Goldin

One more bracket:

XferToDefault.Attributes.Add("onclick", "if(!confirm('Are you sure you want
copy these entries?'))return;")
 
T

tshad

Eliyahu Goldin said:
One more bracket:

XferToDefault.Attributes.Add("onclick", "if(!confirm('Are you sure you want
copy these entries?'))return;")

That works fine now,

How come?

Why didn't the other one work if it worked with the asp:imageButton and
asp:linkbutton?

Thanks,

Tom
 
E

Eliyahu Goldin

Tom,

If an input html control, such as a regular button, or a checkbox, has both
client- and server-side onclick event assigned, asp.net arranges it in the
way

onclick="<javascript for client-side>; <javascript to invoke server-side
handler>;"

In your case you had

onclick="return confirm(...); <javascript to invoke server-side handler>;"

Obviously, the javascript to invoke server-side handler never had its chance
to run.

For ImageButton, which renders as <input type=image>, asp.net apparently
arranges event handlers differently.

Eliyahu
 
H

Hans Kesting

Eliyahu said:
Tom,

If an input html control, such as a regular button, or a checkbox,
has both client- and server-side onclick event assigned, asp.net
arranges it in the way

onclick="<javascript for client-side>; <javascript to invoke
server-side handler>;"

... apart from the ";" inbetween, you have to add that yourself to your (client-side) code!
 
T

tshad

Hans Kesting said:
.. apart from the ";" inbetween, you have to add that yourself to your
(client-side) code!

But I didn't have to for the linkbutton or imagebutton.

Tom
 
T

tshad

Eliyahu Goldin said:
(client-side) code!

No, asp.net does it for you if you have a server-side onclick event set.
You
will find a __doPostBack... call over there.

For example, it will be produced for the following html:

<input type=button runat=server onclick="clientValidate()"
onserverclick="ServerValidate" ... >

So how is:

if(!confirm('Are you sure you want copy these entries?'))return;

different from:

return confirm('Are you sure you want copy these entries?');

That the 1st one works and the 2nd one doesn't?

Tom
 
E

Eliyahu Goldin

If an input html control, such as a regular button, or a checkbox,
.. apart from the ";" inbetween, you have to add that yourself to your
(client-side) code!

No, asp.net does it for you if you have a server-side onclick event set. You
will find a __doPostBack... call over there.

For example, it will be produced for the following html:

<input type=button runat=server onclick="clientValidate()"
onserverclick="ServerValidate" ... >

Eliyahu
 
H

Hans Kesting

tshad said:
So how is:

if(!confirm('Are you sure you want copy these entries?'))return;

different from:

return confirm('Are you sure you want copy these entries?');

That the 1st one works and the 2nd one doesn't?

Tom

return confirm() always returns (true or false)

if (!confirm()) return; only returns (without value) if *not* confirmed
and continues (with the postback presumably) if it *is* confirmed.

Hans Kesting
 
B

Bruce Barker

check boxes do not postback automatically. .net add client script (a call to
__doPostback), after any script you add. if your client code does a return,
the postback script will not be called.

-- bruce (sqlwork.com)
 
T

tshad

Bruce Barker said:
check boxes do not postback automatically. .net add client script (a call
to __doPostback), after any script you add. if your client code does a
return, the postback script will not be called.

But in my case, I am doing an autoPostback=true and it does postback (if you
didn't have the old type of javascript connected to it).

Tom
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top