如何从ASHX处理程序文件调用Java脚本函数



我的功能

<script type="text/javascript">
function methodtocall(id) {
  // My code
}
</script>

我的ASHX页

public class sampleClass : IHttpHandler {
public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/plain";
// here i need to call that method
}
}

是的,我推荐了网络。但没有解决方案。通常,他们给出了从方法到ASHX或从Ajax打电话的解决方案。

有人可以用正确的事情指导我吗?

请参考这样,

ClientScript.RegisterStartupScript(this.GetType(), "methodtocall", script, true);

根据代码,

public class sampleClass : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
var id = "someid"; //implement the id that you want to pass to the Javascript function
ClientScript.RegisterStartupScript(this.GetType(), "methodtocall", id, true);
}
}

希望这会有所帮助..!

参考链接:链接

您可以这样做

context.Response.Write("<script type='text/javascript'>function closeCurrentTab(){var conf=confirm('Are you sure, you want to close this tab?');if(conf==true){close();}}</script>");
context.Response.Write("<script type='text/javascript'>closeCurrentTab();</script>");