简体   繁体   English

从C#静态Web方法调用Javascript函数(页面方法)

[英]Call Javascript function from C# static web method (page method)

I used: 我用了:

ScriptManager.RegisterStartupScript(this, GetType(), "", "MyJSFunction('parameter')", true);

to reach the MyJSFunction function in JavaScript from Page_Load and it worked fine. Page_Load到JavaScript中的MyJSFunction函数,它工作正常。 Now I'm in need of calling lets say the same function but from a static web method, but I get these 2 errors on this & GetType() : 现在我需要调用的让说同样的功能,但是从静态网页的方法,但我得到这2个错误thisGetType()

this: Keyword 'this' is not valid in a static property, static method, or a static field initialized. this:关键字'this'在静态属性,静态方法或初始化的静态字段中无效。

GetType(): An object reference is required for the non-static field, method, or property, 'objec.GetType()'. GetType():非静态字段,方法或属性'objec.GetType()'需要对象引用。

I tried to re-format the code like this: 我试着像这样重新格式化代码:

ScriptManager.RegisterStartupScript((Page)(System.Web.HttpContext.Current.Handler), ((Page)(System.Web.HttpContext.Current.Handler)).GetType(), "", "MyJSFunction('parameter')", true);

No errors shown, but it doesn't reach the JavaScript function. 没有显示错误,但它没有达到JavaScript功能。

Can anyone help? 有人可以帮忙吗?

Call the JavaScript function upon successful completion (callback) of the call to the web method in your JS code . 成功完成(回调) JS代码中对Web方法的调用后,调用JavaScript函数。 You can't "call" client-side code from the code behind; 你不能从后面的代码“调用”客户端代码; you can register a JS function call as a "startup script" but not here as a call to a web method does not involve a postback of the page nor does it cause Page_Load to run. 您可以将JS函数调用注册为“启动脚本”,但不能在此处注册,因为对Web方法的调用不涉及页面的回发,也不会导致Page_Load运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM