从 C# 静态 Web 方法(页面方法)调用 Javascript 函数
Call Javascript function from C# static web method (page method)
我用过:
ScriptManager.RegisterStartupScript(this, GetType(), "", "MyJSFunction('parameter')", true);
从 Page_Load
到达 JavaScript 中的 MyJSFunction
功能,并且工作正常。现在我需要调用相同的函数,但来自静态 Web 方法,但我在 this
& GetType()
:
上遇到了这两个错误
this: Keyword 'this' is not valid in a static property, static method,
or a static field initialized.
GetType(): An object reference is required for the non-static field,
method, or property, 'objec.GetType()'.
我试过像这样重新格式化代码:
ScriptManager.RegisterStartupScript((Page)(System.Web.HttpContext.Current.Handler), ((Page)(System.Web.HttpContext.Current.Handler)).GetType(), "", "MyJSFunction('parameter')", true);
没有显示错误,但没有到达 JavaScript 函数。
有人能帮忙吗?
在您的 JS 代码中成功完成(回调)对网络方法的调用后调用JavaScript函数。您不能 "call" 客户端代码来自后面的代码;您可以将 JS 函数调用注册为 "startup script" 但不能在此处注册,因为对 Web 方法的调用不涉及页面的回发,也不会导致 Page_Load 到 运行.
我用过:
ScriptManager.RegisterStartupScript(this, GetType(), "", "MyJSFunction('parameter')", true);
从 Page_Load
到达 JavaScript 中的 MyJSFunction
功能,并且工作正常。现在我需要调用相同的函数,但来自静态 Web 方法,但我在 this
& GetType()
:
this: Keyword 'this' is not valid in a static property, static method, or a static field initialized.
GetType(): An object reference is required for the non-static field, method, or property, 'objec.GetType()'.
我试过像这样重新格式化代码:
ScriptManager.RegisterStartupScript((Page)(System.Web.HttpContext.Current.Handler), ((Page)(System.Web.HttpContext.Current.Handler)).GetType(), "", "MyJSFunction('parameter')", true);
没有显示错误,但没有到达 JavaScript 函数。
有人能帮忙吗?
在您的 JS 代码中成功完成(回调)对网络方法的调用后调用JavaScript函数。您不能 "call" 客户端代码来自后面的代码;您可以将 JS 函数调用注册为 "startup script" 但不能在此处注册,因为对 Web 方法的调用不涉及页面的回发,也不会导致 Page_Load 到 运行.