最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to run the javascript fn using OnClick button event (asp.net) - Stack Overflow

programmeradmin2浏览0评论

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 
}
发布评论

评论列表(0)

  1. 暂无评论