ImageButton OnClientClick Events Not Firing on Client

G

Guest

I am having trouble getting client-side events to fire on ImageButtons.
Instead I am seeing postbacks. The client-side handlers end with "return
false;" but they are never called.

<asp:ImageButton id="myButton" runat="server"
OnClientClick="myButtonClick();" src=... ></asp:ImageButton>

In the javascript:

function myButtonClick()
{
alert("myButton clicked");
return false;
}

What can the problem be?

Thank you!
Eagle
 
G

Guest

Try to add "return" to tell the button to depend on the return value of the
script that is being called.

instead of: OnClientClick="myButtonClick();"
use this: OnClientClick="return myButtonClick();"

Cheers!
 
G

Guest

What?

Eliyahu Goldin said:
View html source for the rendered page. What is there for myButton?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"(e-mail address removed)"
I am having trouble getting client-side events to fire on ImageButtons.
Instead I am seeing postbacks. The client-side handlers end with "return
false;" but they are never called.

<asp:ImageButton id="myButton" runat="server"
OnClientClick="myButtonClick();" src=... ></asp:ImageButton>

In the javascript:

function myButtonClick()
{
alert("myButton clicked");
return false;
}

What can the problem be?

Thank you!
Eagle
 
E

Eliyahu Goldin

When viewing the page in the browser, rightclick the browser window and
choose "View Source" (that is in IE, other browsers may have different name
for the same function). You will see the html for the page you are viewing.
Then find in the html a control with id containing "myButton". You will see
what html code asp.net produced for the button. It may help you in
understanding the problem.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jon Gonzales said:
What?

Eliyahu Goldin said:
View html source for the rendered page. What is there for myButton?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"(e-mail address removed)"
I am having trouble getting client-side events to fire on ImageButtons.
Instead I am seeing postbacks. The client-side handlers end with
"return
false;" but they are never called.

<asp:ImageButton id="myButton" runat="server"
OnClientClick="myButtonClick();" src=... ></asp:ImageButton>

In the javascript:

function myButtonClick()
{
alert("myButton clicked");
return false;
}

What can the problem be?

Thank you!
Eagle
 
G

Guest

Definitely not, it doesn't make sense viewing the rendered script for the
button, just to the return thing, it will definitely fix the problem.

Correct me if I'm wrong.

Cheers!

Eliyahu Goldin said:
When viewing the page in the browser, rightclick the browser window and
choose "View Source" (that is in IE, other browsers may have different name
for the same function). You will see the html for the page you are viewing.
Then find in the html a control with id containing "myButton". You will see
what html code asp.net produced for the button. It may help you in
understanding the problem.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jon Gonzales said:
What?

Eliyahu Goldin said:
View html source for the rendered page. What is there for myButton?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"(e-mail address removed)"
I am having trouble getting client-side events to fire on ImageButtons.
Instead I am seeing postbacks. The client-side handlers end with
"return
false;" but they are never called.

<asp:ImageButton id="myButton" runat="server"
OnClientClick="myButtonClick();" src=... ></asp:ImageButton>

In the javascript:

function myButtonClick()
{
alert("myButton clicked");
return false;
}

What can the problem be?

Thank you!
Eagle
 
E

Eliyahu Goldin

You are right, adding "return..." is necessary. But I am referring to the
OP's statement:

If the event handler indeed is never called, viewing html will help.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jon Gonzales said:
Definitely not, it doesn't make sense viewing the rendered script for the
button, just to the return thing, it will definitely fix the problem.

Correct me if I'm wrong.

Cheers!

Eliyahu Goldin said:
When viewing the page in the browser, rightclick the browser window and
choose "View Source" (that is in IE, other browsers may have different
name
for the same function). You will see the html for the page you are
viewing.
Then find in the html a control with id containing "myButton". You will
see
what html code asp.net produced for the button. It may help you in
understanding the problem.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jon Gonzales said:
What?

:

View html source for the rendered page. What is there for myButton?

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"(e-mail address removed)"
message
I am having trouble getting client-side events to fire on
ImageButtons.
Instead I am seeing postbacks. The client-side handlers end with
"return
false;" but they are never called.

<asp:ImageButton id="myButton" runat="server"
OnClientClick="myButtonClick();" src=... ></asp:ImageButton>

In the javascript:

function myButtonClick()
{
alert("myButton clicked");
return false;
}

What can the problem be?

Thank you!
Eagle
 
A

Aaron

I'm having the exact same issue as OP's. I have a master page with an
"authentication" control at the top that contains a username and
password text inputs. There is a "login" ImageButton. The login image
button has postback event (click event) and OnClientClick function
call that simply "return false;". If I make
OnClientClick="alert('hello'); return false;", it does what I expect.
However, if I do OnClientClick="return myFunction();" where
myFunction() { return false; }, I get a postback. I'm curious if the
original post developer is also using master pages.

-ak
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top