Javascript 如何从c#调用javascript函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2693601/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:28:45  来源:igfitidea点击:

How to call javascript function from c#

asp.netjavascriptevents

提问by Partha

I like to call a JavaScript function from c#. Can any one can give me code snippet.

我喜欢从 c# 调用 JavaScript 函数。任何人都可以给我代码片段。

More detail...

更多详情...

I have a asp.net page which has a asp button. when i click that button, i like to call javascript function.

我有一个 asp.net 页面,它有一个 asp 按钮。当我点击那个按钮时,我喜欢调用 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函数......

回答by Mikael Svenson

For an asp:buttonyou use OnClientClick

对于asp:button您使用OnClientClick

<asp:Button id="myid" runat="server" OnClientClick="alert('test')" />

回答by Craig Walker

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. 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# 调用 JavaScript”的定义。您正在做的是让您的 C#/ASPX 代码生成 HTML/JavaScript,然后浏览器将其解释为任何其他 HTML/JS(无论它是如何生成的)。

Perhaps you could explain what you're trying to do a bit more.

也许你可以解释一下你想要做的更多。

回答by Fairy_G

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); 

回答by sidney.andrews

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()

您不能从 ASP.NET C# 代码隐藏中“调用”Javascript 函数。您可以为网页编写额外的 Javascript。当页面被发送回用户并且 Javascript 存在时,你的代码隐藏已经消失了。您可以写出文字或执行 Response.Write()

Response.Write("<script language='javascript'>alert('Hellow World');</script>");

回答by Craig Walker

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:

Sarathi,根据您最近的更新,尚不清楚您是否需要任何 C# 交互。您<button>似乎是严格的客户端(即:HTML),其中没有 ASP.NET 交互。要调用您的 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. That means it could be defined in any of:

请注意,mySaveFunction()只需要在浏览器的当前页面的加载堆栈中定义即可。这意味着它可以定义为:

  • 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.
  • ASPX 页面包含 <button>
  • 当前 ASPX 页的母版页
  • 当前 ASPX 页面加载的用户控件(或 MVC 部分)之一
  • 由上述之一加载的外部 JavaScript 文件。

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.

最后,我想重申一下,没有什么特别针对 C#/ASP.NET 的。您可以对任何语言/框架执行相同操作,包括静态 HTML 文件。您的问题似乎完全依赖于 JavaScript。

回答by Prabu Parthipan

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..

您可以从页面背后的代码调用javascript函数..例如,您在javasript中有closewindow函数定义部分..如果您想执行该函数,您可以在页面代码的任何点击事件中编写以下代码..

ClientScript.RegisterClientScriptBlock(GetType(), "close", "<script language=javascript>Closewindow();</script>", false);

回答by Blessed Geek

For the window object:
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

窗口执行脚本

For the page pbject:
http://msdn.microsoft.com/en-us/library/dfbt9et1%28v=VS.71%29.aspx

对于页面 pbject:http:
//msdn.microsoft.com/en-us/library/dfbt9et1%28v=VS.71%29.aspx

RegisterClientScriptBlock

注册客户端脚本块

RegisterOnSubmitStatement

注册提交声明

RegisterStartupScript

注册启动脚本

etc ...

等等 ...