这里我的简单示例代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace PopUpFromCsFile
{
public class PopUpWindow
{
public void PopUpFromCsFile()
{
string str1 = "<script> $.ajax({" +
"type: 'GET'," +
"url: 'Service/Class1.cs/callfromjs'," +
"data: '{}'," + "success: function () { getDetails(); } " +
"});</script>";
page.ClientScript.RegisterStartupScript(this.GetType(), "script1", str1);
}
[WebMetod]
public string CallFromJs()
{
return "santosh";
}
public void getDetails()
{
string str = "<script>alert('Hai');</script>";
System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.Handler;
page.ClientScript.RegisterStartupScript(this.GetType(), "script2", str);
}
}
}
解释
在 PopUpFromCsFile() 中,我为 Ajax+WebMethod 编写了代码。Url 路径是否正确,这里我无法从脚本调用 getDetails()。