admin管理员组文章数量:1307165
i have a form and a button a form:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="SubmitData" />
i have a method in my c# program. the method is called SubmitData
however i would also like to run a javascript function on this button click as well. how do i do this?
here is my javascript function:
var selectedLanguages = new Array();
jQuery('#lstProblems option:selected').each(function() {
selectedLanguages.push(jQuery(this).val());
});
i got it from here: jquery listbox return what user selected
how do i run it ? do i have to put it in <script></script>
and do some_Function(etc...) ?
i have a form and a button a form:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="SubmitData" />
i have a method in my c# program. the method is called SubmitData
however i would also like to run a javascript function on this button click as well. how do i do this?
here is my javascript function:
var selectedLanguages = new Array();
jQuery('#lstProblems option:selected').each(function() {
selectedLanguages.push(jQuery(this).val());
});
i got it from here: jquery listbox return what user selected
how do i run it ? do i have to put it in <script></script>
and do some_Function(etc...) ?
3 Answers
Reset to default 5you should use the OnClientClick='myJsFunc();'
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="SubmitData" OnClientClick="aaa()" />
<script type="text/javascript">
function aaa()
{
var selectedLanguages = new Array();
jQuery('#lstProblems option:selected').each(function() {
selectedLanguages.push(jQuery(this).val());
});
}
</script>
You can use OnClientClick event
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Your javascript function" OnClick="SubmitData" />
Set up your server side event code the way it seems to be already, then in the Page_Load method of your code behind add the following line:
Button1.Attributes.Add("onclick","yourJavascriptFunction();");
EDIT: To run the function from your edited question, simply create a function of the same name in your javascript file. Something like this:
<script type="text/javascript">
function yourJavascriptFunction()
{
var selectedLanguages = new Array();
jQuery('#lstProblems option:selected').each(function() {
selectedLanguages.push(jQuery(this).val());
});
}
</script>
本文标签: caspnet run javascript on clickStack Overflow
版权声明:本文标题:c# - asp.net: run javascript on click? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741834499a2400132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论