Calling JavaScript Function From CodeBehind

2019-01-01 10:41发布

Can someone provide good examples of calling a JavaScript function From CodeBehind and Vice-versa?

19条回答
浅入江南
2楼-- · 2019-01-01 10:53

You may try this :

Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);
查看更多
高级女魔头
3楼-- · 2019-01-01 10:55

Try This in Code Behind and it will worked 100%

string script = "window.onload = function() { YourJavaScriptFunctionName(); };";
ClientScript.RegisterStartupScript(this.GetType(), "YourJavaScriptFunctionName", script, true);
查看更多
有味是清欢
4楼-- · 2019-01-01 10:55

You can use literal:

this.Controls.Add(new LiteralControl("<script type='text/javascript'>myFunction();</script>"));
查看更多
裙下三千臣
5楼-- · 2019-01-01 10:56

You can expose C# methods on codebehind pages to be callable via JavaScript by using the ScriptMethod attribute.

You cannot call JavaScript from a CodeBehind - that code exists solely on the client.

查看更多
笑指拈花
6楼-- · 2019-01-01 10:57

IIRC Code Behind is compiled serverside and javascript is interpreted client side. This means there is no direct link between the two.

What you can do on the other hand is have the client and server communicate through a nifty tool called AJAX. http://en.wikipedia.org/wiki/Asynchronous_JavaScript_and_XML

查看更多
春风洒进眼中
7楼-- · 2019-01-01 10:58
ScriptManager.RegisterStartupScript(Page, GetType(), "JavaFunction", "AlertError();", true);

using your function is enough

查看更多
登录 后发表回答