admin管理员组文章数量:1180451
I like to call a JavaScript function from c#. Can any one can give me code snippet.
More detail...
I have a asp page which has a asp button. when i click that button, i like to call javascript function.
like wise....
in my asp page,
<button id="save" onclick="i like to call a method resides in asp page'>Save</button>
More and more details... when click the asp button, i like to perform some server side action and then like to call a javascript function from there itself...
I like to call a JavaScript function from c#. Can any one can give me code snippet.
More detail...
I have a asp.net page which has a asp button. when i click that button, i like to call javascript function.
like wise....
in my asp.net page,
<button id="save" onclick="i like to call a method resides in asp.net page'>Save</button>
More and more details... when click the asp.net button, i like to perform some server side action and then like to call a javascript function from there itself...
Share Improve this question edited Apr 22, 2010 at 20:10 3Dave 29k18 gold badges90 silver badges151 bronze badges asked Apr 22, 2010 at 18:55 ParthaPartha 2,1926 gold badges24 silver badges31 bronze badges 4- 4 Is this from within an ASP.NET page? A Desktop app? Need more context for what type of C# project this is being called from. – Daniel DiPaolo Commented Apr 22, 2010 at 18:59
- 2 You may want to flesh out your answer a bit; it's a bit vague. – Craig Walker Commented Apr 22, 2010 at 19:00
- Are you asking about how to execute a .js file on the local machine (i.e. Windows Script Host) or are you talking about doing something to the browser in an ASP.NET application? Both of these seem a bit odd, to be honest. Be more specific in your question. Give us examples of what you've tried to do. What are you trying to achieve? – Damian Powell Commented Apr 22, 2010 at 19:00
- 2 You're not technically calling Javascript from C#... you just want to have an asp.net button call Javascript, Mikael Svenson's answer is the way to accomplish that – sidney.andrews Commented Apr 22, 2010 at 19:36
7 Answers
Reset to default 9For an asp:button
you use OnClientClick
<asp:Button id="myid" runat="server" OnClientClick="alert('test')" />
On the assumption that you're coding in ASP.NET (including MVC), calling a JavaScript function would mean embedding the call in JavaScript into your ASPX code, like so:
<script type="text/javascript">
doSomething();
</script>
You do have the opportunity to pass information from your C# to the JS call, just as you would have any other code alter the results of your ASPX:
<script type="text/javascript">
doSomething("<%= GetSomeTextFromCSharp(); %>");
</script>
This is really stretching the definition of "calling JavaScript from C#" though. What you're doing is having your C#/ASPX code generate HTML/JavaScript, which the browser then interprets as it would any other HTML/JS (regardless of how it was generated).
Perhaps you could explain what you're trying to do a bit more.
i tried with this code it works for me check whether it helps
1)
Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "alert('Informations');", true);
The other way is call the javascript method which is written in source page
Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "xyz();", true);
You can't "call" a Javascript function from ASP.NET C# code-behind. You can write additional Javascript to the webpage. By the time the page is sent back to the user and the Javascript exists, your code-behind is gone. You can write out to a Literal or do a Response.Write()
Response.Write("<script language='javascript'>alert('Hellow World');</script>");
Sarathi, based on your recent update, it's not clear that you need any C# interaction at all. Your <button>
appears to be strictly client-side (ie: HTML) with no ASP.NET interaction within it. To call your JavaScript function you'd attach the function call to the onclick attribute of the button tag:
<button id="save" onclick="mySaveFunction();'>Save</button>
Note that mySaveFunction()
just needs to be defined in the browser's load stack for the current page. That means it could be defined in any of:
- The ASPX page that holds the
<button>
- The Master page for the current ASPX page
- One of the User controls (or MVC partials) loaded by the current ASPX page
- An external JavaScript file that's loaded by one of the above.
Lastly, I'd just like to reiterate that there's nothing particularly C#/ASP.NET-specific about this. You could do the same with any language/framework, including static HTML files. Your question appears to be entirely JavaScript-dependent.
For the window object:
http://msdn.microsoft.com/en-us/library/ms536420%28VS.85%29.aspx
window.execScript
For the page pbject:
http://msdn.microsoft.com/en-us/library/dfbt9et1%28v=VS.71%29.aspx
RegisterClientScriptBlock
RegisterOnSubmitStatement
RegisterStartupScript
etc ...
you can call javascript function from code behind page ..for example you have closewindow function definition part in javasript..if you want to execute that function,you can write following codings in any click event in code behind page..
ClientScript.RegisterClientScriptBlock(GetType(), "close", "<script language=javascript>Closewindow();</script>", false);
本文标签: aspnetHow to call javascript function from cStack Overflow
版权声明:本文标题:asp.net - How to call javascript function from c# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738173490a2067147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论