javascript 如何从背后的c#代码调用javascript函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11823384/
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 call a javascript function from c# code behind
提问by user1575995
initially a table is invisible in the ASP.NET page. on button click event, it should go to the code behind and from there i need to call a function in javascript. In that javascript function i should make the table to be visible. Is this possible?? Somebody please help me out
最初,表格在 ASP.NET 页面中是不可见的。在按钮单击事件上,它应该转到后面的代码,从那里我需要在 javascript 中调用一个函数。在那个 javascript 函数中,我应该使表格可见。这可能吗??有人请帮帮我
回答by Vinod
Try this:
试试这个:
Page.ClientScript.RegisterStartupScript(GetType(), "MyKey", "Myfunction();", true);
回答by Ram Singh
try the following code
试试下面的代码
ScriptManager.RegisterStartupScript(this, this.GetType(), "isActive", "alert('hello');", true);
Here "this" is used for control by which you want to fire this. this.Gettype()is used get type of Client Script "isActive" it is the unique key. After all these the javascript code. it could be function whatever you want to do. and lastly there is truethat ask you either you want to script tag for the javascript code you are writing here or not.
这里“ this”用于控制您要触发它的控制。 this.Gettype()用于获取客户端脚本“ isActive”的类型,它是唯一键。毕竟这些 javascript 代码。无论您想做什么,它都可以发挥作用。最后,确实会询问您是否要为您在此处编写的 javascript 代码编写标签。
回答by user1532340
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "HelloWorld", "HelloWorld();", true);
回答by sohaib
Try This
试试这个
string str="<script>alert(\"ok\");</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", str, false);
string str="<script>alert(\"ok\");</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", str, false);
回答by Pravin Pawar
From code behind you can invoke javascript something like this
从后面的代码中,您可以像这样调用 javascript
ScriptManager.RegisterStartupScript(Page, GetType(Page), "ScriptName", 'document.getelementById("tableId").display = inline;' , True)
But i would recommend to do it on client side without posting a page. Post the page only if you need to access server resources.
但我会建议在客户端进行而不发布页面。仅在需要访问服务器资源时发布页面。
回答by Moons
You can call javascript function using code behind as:
您可以使用以下代码调用 javascript 函数:
ScriptManager.RegisterStartupScript(page, typeof(Page), Guid.NewGuid().ToString(), "callJavaScriptFunction();", true);
回答by Parv Sharma
calling javascript functions from code behind is not possible(if we leave apart the newer sockets
).
after the page has loaded once fro the web server then there is no way for the server to know what data to send to which user..
you should probably try out sending an ajax request
and execute some code depending upon the response got from the server.
从后面的代码调用 javascript 函数是不可能的(如果我们不考虑较新的sockets
)。
在页面从 Web 服务器加载一次之后,服务器就无法知道将哪些数据发送给哪个用户..
您应该尝试ajax request
根据从服务器获得的响应发送并执行一些代码。