ImageButton OnClientClick Events Not Firing on Client

Discussion in 'ASP .Net' started by =?Utf-8?B?RWFnbGVSZWRASGlnaEZseWluZ0JpcmRzLmNvbQ==, Nov 14, 2007.

  1. 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
     
    =?Utf-8?B?RWFnbGVSZWRASGlnaEZseWluZ0JpcmRzLmNvbQ==, Nov 14, 2007
    #1
    1. Advertising

  2. 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!

    "" wrote:

    > 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
     
    =?Utf-8?B?Sm9uIEdvbnphbGVz?=, Nov 14, 2007
    #2
    1. Advertising

  3. 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


    ""
    <> wrote in message
    news:...
    >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
     
    Eliyahu Goldin, Nov 14, 2007
    #3
  4. What?

    "Eliyahu Goldin" wrote:

    > 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
    >
    >
    > ""
    > <> wrote in message
    > news:...
    > >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

    >
    >
    >
     
    =?Utf-8?B?Sm9uIEdvbnphbGVz?=, Nov 15, 2007
    #4
  5. 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" <> wrote in message
    news:...
    > What?
    >
    > "Eliyahu Goldin" wrote:
    >
    >> 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
    >>
    >>
    >> ""
    >> <> wrote in message
    >> news:...
    >> >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

    >>
    >>
    >>
     
    Eliyahu Goldin, Nov 15, 2007
    #5
  6. 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" wrote:

    > 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" <> wrote in message
    > news:...
    > > What?
    > >
    > > "Eliyahu Goldin" wrote:
    > >
    > >> 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
    > >>
    > >>
    > >> ""
    > >> <> wrote in message
    > >> news:...
    > >> >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
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?Sm9uIEdvbnphbGVz?=, Nov 15, 2007
    #6
  7. You are right, adding "return..." is necessary. But I am referring to the
    OP's statement:

    >> >> > ... The client-side handlers end with
    >> >> > "return
    >> >> > false;" but they are never called.


    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" <> wrote in message
    news:...
    > 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" wrote:
    >
    >> 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" <> wrote in message
    >> news:...
    >> > What?
    >> >
    >> > "Eliyahu Goldin" wrote:
    >> >
    >> >> 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
    >> >>
    >> >>
    >> >> ""
    >> >> <> wrote in
    >> >> message
    >> >> news:...
    >> >> >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
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
     
    Eliyahu Goldin, Nov 15, 2007
    #7
  8. =?Utf-8?B?RWFnbGVSZWRASGlnaEZseWluZ0JpcmRzLmNvbQ==

    Aaron Guest

    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
     
    Aaron, Nov 30, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Clark
    Replies:
    2
    Views:
    493
    Darren Clark
    Jun 25, 2004
  2. Steve

    Onclientclick firing twice?

    Steve, Feb 7, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    911
  3. Nathan Sokalski
    Replies:
    3
    Views:
    7,561
    Teemu Keiski
    Aug 1, 2007
  4. Nathan Sokalski

    Problem with ImageButton.OnClientClick when DataBinding

    Nathan Sokalski, Jul 31, 2007, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    792
    Teemu Keiski
    Aug 1, 2007
  5. rum23

    asp:ImageButton and onClientClick and page refresh

    rum23, Nov 28, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    700
Loading...

Share This Page