C# 如何使用 OnClick 按钮事件运行 javascript fn (asp.net)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9081510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run the javascript fn using OnClick button event (asp.net)
提问by Murthy
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
我有一个输入按钮,单击时我调用了一个 javascript 函数。但我想向按钮添加图像所以我使用了图像按钮,但我无法从图像按钮调用 Javascript 函数。单击图像按钮时如何运行 javascript fn
<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'" />
采纳答案by Igarioshka
回答by Vitaly Zemlyansky
you can bind click event via jQuery, like this
你可以通过jQuery绑定点击事件,就像这样
$("#id_of_your_imagebutton").click(function(){
//your code for event here
});
If you use asp.net 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
如果您使用 asp.net 4,您可以将 ClientIDMode="Static" 设置为您的 ImageButton 并为 ID 获取“发送”,或者如果您使用早期版本,则可以通过 FireBug 获取 ID
回答by Ravi Gadag
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
}

