1

I'm making .aspx file and using jQuery to do key press function. I made few <asp:Imagebutton> so write specific button id in jQuery part.

$(document).keypress(function (e) {
                if (e.which === 13) {
                    $("#ImageSave").click();
                }
            });

I wrote $("#ImageSave") following aspx button id. but asp:button id in html is different with my original button id. so I change jQuery code id part. $("#ImageSave").click(); to $("#MainContent_ImageSave"). But its click event is not fired.

asp.net

<asp:ImageButton ID="ImageSave" runat="server" imageurl="img/button_save.jpg"  
       AutoPostBack="True" onclick="ImageSave_Click" />    

html

<input name="ct100$MainContent$ImageSave" id="MainContent_ImageSave" 
     type="image" src="img/button_save.jpg" autopostback="True"></input>   

I think this problem because of using asp id in jQuery way is wrong. Would be nice if you can help me with this or atleast point me to the right direction :)

naanace
  • 363
  • 1
  • 4
  • 17

1 Answers1

2

You can use this as id

<%= ImageSave.ClientID %>

so instead of this

 $("#ImageSave").click();

use

 $("#<%= ImageSave.ClientID %>").click();

Or you can use ClientIDMode="Static" so that the id doesn't change at runtime. If you are using asp.net 4.0 and above.

<asp:ImageButton ID="ImageSave" runat="server" imageurl="img/button_save.jpg"  
   AutoPostBack="True" onclick="ImageSave_Click" ClientIDMode="Static" />    
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • thanks quick and clarify answer! But still doesn't work. when I press enter key, other button **Calendar** is pressed. `` – naanace Feb 02 '15 at 04:28
  • `$(document).ready(function () {` //I wrote above code here `});` I really don't know what's problem OTL.... – naanace Feb 02 '15 at 04:35
  • No I didn't. Do I need?? – naanace Feb 02 '15 at 04:56
  • instead of this `$("#ImageSave").click();` try `$("#ImageSave").trigger("click");` it should work. – शेखर Feb 02 '15 at 05:11
  • I tested all of your reply that click() and trigger event, and finally I did wrong point. It works! but just calendar button works. I think it's depend on sequence of button. `calendar `button is positioned in front of `ImageSave` button. Do you know why? – naanace Feb 02 '15 at 05:49