[英]How to call javascript function from c#
I like to call a JavaScript function from c#. 我喜欢从c#调用JavaScript函数。 Can any one can give me code snippet.
任何人都可以给我代码片段。
More detail... 更多详情...
I have a asp.net page which has a asp button. 我有一个asp.net页面,它有一个asp按钮。 when i click that button, i like to call javascript function.
当我点击该按钮时,我想调用javascript函数。
like wise.... 喜欢......
in my asp.net page, 在我的asp.net页面中,
<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... 越来越多的细节...当点击asp.net按钮时,我喜欢执行一些服务器端操作,然后喜欢从那里调用一个javascript函数...
对于asp:button
您使用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: 假设您在ASP.NET(包括MVC)中编码,调用JavaScript函数意味着将JavaScript中的调用嵌入到ASPX代码中,如下所示:
<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: 您确实有机会将信息从C#传递给JS调用,就像您可以使用任何其他代码更改ASPX的结果一样:
<script type="text/javascript">
doSomething("<%= GetSomeTextFromCSharp(); %>");
</script>
This is really stretching the definition of "calling JavaScript from C#" though. 这实际上扩展了“从C#调用JavaScript”的定义。 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).
您正在做的是让您的C#/ ASPX代码生成HTML / JavaScript,然后浏览器将其解释为任何其他HTML / JS(无论它是如何生成的)。
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) 1)
Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "alert('Informations');", true);
The other way is call the javascript method which is written in source page 另一种方法是调用源代码页中编写的javascript方法
Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "xyz();", true);
You can't "call" a Javascript function from ASP.NET C# code-behind. 您不能从ASP.NET C#代码隐藏“调用”Javascript函数。 You can write additional Javascript to the webpage.
您可以将其他Javascript写入网页。 By the time the page is sent back to the user and the Javascript exists, your code-behind is gone.
当页面被发送回用户并且Javascript存在时,您的代码隐藏已经消失。 You can write out to a Literal or do a Response.Write()
你可以写出一个文字或做一个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. Sarathi,根据您最近的更新,您不清楚是否需要任何C#交互。 Your
<button>
appears to be strictly client-side (ie: HTML) with no ASP.NET interaction within it. 您的
<button>
似乎是严格的客户端(即:HTML),其中没有ASP.NET交互。 To call your JavaScript function you'd attach the function call to the onclick attribute of the button tag: 要调用JavaScript函数,您需要将函数调用附加到按钮标记的onclick属性:
<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. 请注意,
mySaveFunction()
只需要在浏览器的当前页面的加载堆栈中定义。 That means it could be defined in any of: 这意味着它可以在以下任何一个中定义:
<button>
<button>
的ASPX页面 Lastly, I'd just like to reiterate that there's nothing particularly C#/ASP.NET-specific about this. 最后,我想重申一下,没有特别针对此的C#/ ASP.NET特定内容。 You could do the same with any language/framework, including static HTML files.
您可以对任何语言/框架执行相同的操作,包括静态HTML文件。 Your question appears to be entirely JavaScript-dependent.
您的问题似乎完全取决于JavaScript。
For the window object: 对于window对象:
http://msdn.microsoft.com/en-us/library/ms536420%28VS.85%29.aspx http://msdn.microsoft.com/en-us/library/ms536420%28VS.85%29.aspx
window.execScript window.execScript
For the page pbject: 对于页面pbject:
http://msdn.microsoft.com/en-us/library/dfbt9et1%28v=VS.71%29.aspx http://msdn.microsoft.com/en-us/library/dfbt9et1%28v=VS.71%29.aspx
RegisterClientScriptBlock 的RegisterClientScriptBlock
RegisterOnSubmitStatement RegisterOnSubmitStatement
RegisterStartupScript 的RegisterStartupScript
etc ... 等......
你可以从页面后面的代码调用javascript函数..例如你在javasript中有closewindow函数定义部分..如果你想执行该函数,你可以在页面后面的代码中的任何点击事件中编写以下编码。
ClientScript.RegisterClientScriptBlock(GetType(), "close", "<script language=javascript>Closewindow();</script>", false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.