admin管理员组

文章数量:1355532

I had a input button to which I called a javascript function when clicked. But I want to add images to the button So I used a Image button but I am not able to call the Javascript function from the image button. How to run the javascript fn when the image button is clicked

<input id="send" type="button" value=" Preview on iPhone " onclick="javascript:preview()">


   <asp:ImageButton  OnClick="preview()"  ID="send" runat="server" ImageUrl="Styles/Images/preview_iphone.png"
                                     OnMouseOut="this.src='Styles/Images/preview_iphone.png'"
                                    OnMouseOver="this.src='Styles/Images/preview_iphone_over.png'" />

I had a input button to which I called a javascript function when clicked. But I want to add images to the button So I used a Image button but I am not able to call the Javascript function from the image button. How to run the javascript fn when the image button is clicked

<input id="send" type="button" value=" Preview on iPhone " onclick="javascript:preview()">


   <asp:ImageButton  OnClick="preview()"  ID="send" runat="server" ImageUrl="Styles/Images/preview_iphone.png"
                                     OnMouseOut="this.src='Styles/Images/preview_iphone.png'"
                                    OnMouseOver="this.src='Styles/Images/preview_iphone_over.png'" />
Share Improve this question asked Jan 31, 2012 at 14:47 Murthy Murthy 1,55211 gold badges31 silver badges45 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

try the OnClientClick="javascript:preview()" that's the one for the client side click.

from here

you can bind click event via jQuery, like this

$("#id_of_your_imagebutton").click(function(){
//your code for event here
});

If you use asp 4, you can set ClientIDMode="Static" to your ImageButton and get "send" for ID, or you can get ID via FireBug if you use earler versions

you can try like this, in code behind file.

Protected void Page_Load(object sender, EventArgs e) 
{
   send.Attributes.Add("onclick", "javascript:preview(); return false;" ) // retruning false will avoid postback 
}

本文标签: cHow to run the javascript fn using OnClick button event (aspnet)Stack Overflow