如何从方法内部调用javascript函数?

时间:2020-03-06 14:55:54  来源:igfitidea点击:

我在...里面

public class bgchange : IMapServerDropDownBoxAction
{
    void IServerAction.ServerAction(ToolbarItemInfo info)
    {
     Some code...

在"一些代码"之后,我想触发

[WebMethod]
public static void DoSome()
{

}

这触发了一些JavaScript。这可能吗?

好的,在这里切换方法。我能够打电话给dosome();触发但未触发javascript的代码。我尝试使用registerstartupscript方法,但不完全了解如何实现它。这是我尝试过的:

public class bgchange : IMapServerDropDownBoxAction
{

void IServerAction.ServerAction(ToolbarItemInfo info)
{
    ...my C# code to perform on dropdown selection...
        //now I want to trigger some javascript...
        // Define the name and type of the client scripts on the page.
        String csname1 = "PopupScript";
        Type cstype = this.GetType();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        // Check to see if the startup script is already registered.
        if (!cs.IsStartupScriptRegistered(cstype, csname1))
        {
            String cstext1 = "alert('Hello World');";
            cs.RegisterStartupScript(cstype, csname1, cstext1, true);
        }
}

}

我从msdn示例中获得了registerstartupscript代码。显然,我没有正确实现它。当前vs表示:"非静态字段,方法或者属性'System.Web.UI.Page.ClientScript.get'需要对象引用,引用代码段" Page.Clientscript;"谢谢。

解决方案

自从我最初回答以来,这个问题发生了巨大变化。

现在我认为答案是否定的。但是我可能是错的。

我不确定我是否完全理解我们要尝试执行的操作,客户端是什么以及什么不是...的顺序。

但是,我们可以在页面上添加一个启动javascript方法,然后将其称为WebMethod。通过javascript调用WebMethod时,可以添加回调函数,然后在WebMethod返回时调用该函数。

如果在页面上添加ScriptManager标记,则可以通过Javascript调用页面中定义的WebMethod。

<asp:ScriptManager ID="scriptManager1" 
    runat="server" EnablePageMethods="true" />

从Page_Load函数,我们可以将调用添加到WebMethod。

Page.ClientScript.RegisterStartupScript(
    this.GetType(), 
    "callDoSome",
    "PageMethods.DoSome(Callback_Function, null)", 
    true);

Callback_Function表示将在调用WebMethod后执行的javascript函数。

<script language="javascript" type="text/javascript">
    function Callback_Function(result, context) {
        alert('WebMethod was called');
    }
</script>

编辑:

找到了Web ADF控件的此链接。这是我们正在使用的吗?

在该页面上,类似这样的内容将执行JavaScript回调。

public void ServerAction(ToolbarItemInfo info) {
    string jsfunction = "alert('Hello');";
    Map mapctrl = (Map)info.BuddyControls[0];
    CallbackResult cr = new CallbackResult(null, null, "javascript", jsfunction);
    mapctrl.CallbackResults.Add(cr);
}

我们不能直接从服务器在浏览器中调用方法。但是,我们可以将javascript函数添加到将在浏览器中执行方法的响应中。

在ServerAction方法中,执行以下操作

string script = "<script language='javascript'>\n";
script += "javascriptFunctionHere();\n";
script += "</script>";

Page.RegisterStartupScript("ForceClientToCallServerMethod", script);

更多信息在这里

如果以上是我们调用RegisterStartupScript的方式,则它将无法正常工作,因为我们没有对" Page"对象的引用。

示例中的bgchange扩展了Object(假设IMapServerDropDownBoxAction是一个接口),这意味着没有Page实例供我们参考。

这样我们就可以从Asp.Net页面或者UserControl进行完全相同的操作,因为它是有效的引用,因此可以正常工作。

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(
            this.GetType(),
            "helloworldpopup",
            "alert('hello world');",
            true);          
    }
}