Mr A
Mr A

Reputation: 6768

Invoking a javascript function on image button click in asp.net

I am trying to invoke a jquery function when a client press the image button, below is the code:

<asp:ImageButton ID="startUploadLink" runat="server" 
    onclick="startUploadLink_Click" />

    $("#startUploadLink").click(function () {
         $('#<%=FileUpload1.ClientID%>').uploadifyUpload();


         return false;
     });

want to invoke the above function when a user click on image button , also want some code behind logic of onclick image button should work in conjuction with the jquery function, is there any way of achieving it. Any help or suggestions will be highly appreciated.

Upvotes: 1

Views: 11363

Answers (2)

Jamie Treworgy
Jamie Treworgy

Reputation: 24334

Why not just use jQuery to bind the click event? You are already sort of doing that, but probably it doesn't work because you aren't looking for the client ID. I'm not sure why you did the right thing for finding FileUpload1 but not for binding the click event, but this should be all you need to do:

<asp:ImageButton ID="startUploadLink" runat="server" 
    onclick="startUploadLink_Click" />
    $('#<%=startUploadLink.ClientID %>').click(function () {
         $('#<%=FileUpload1.ClientID%>').uploadifyUpload();
 return false;
     });

jQuery bindings will override onclientclick but there's not much reason why you should use both. It's better to just use jQuery for binding events if you're already using it. They will work fine with codebehind/postbacks as well. If you want to PREVENT the form from posting from a jQuery bound event, then use e.preventDefault() in the javascript, otherwise, the default action (e.g. the ASP.NET postback function called from href) will occur.

Upvotes: 1

Andrew
Andrew

Reputation: 5277

Try using onclientclick on the imagebutton

<asp:ImageButton ID="startUploadLink" runat="server"      onclientclick="startUploadLink_Click" />

Upvotes: 1

Related Questions