如何从方法内部调用 javascript 函数? [英] How can I call a javascript function from inside a method?

查看:21
本文介绍了如何从方法内部调用 javascript 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在...里面

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

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

and after "some code" I want to trigger

[WebMethod]
public static void DoSome()
{

}

这会触发一些 javascript.这可能吗?

Which triggers some javascript. Is this possible?

好的,在这里切换方法.我能够调用 dosome();哪个触发但没有触发javascript.我曾尝试使用 registerstartupscript 方法,但不完全了解如何实现它.这是我尝试过的:

Ok, switch methods here. I was able to call dosome(); which fired but did not trigger the javascript. I have tried to use the registerstartupscript method but don't fully understand how to implement it. Here's what I tried:

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;"谢谢.

I got the registerstartupscript code from an msdn example. Clearly I am not implementing it correctly. Currently vs says "An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.ClientScript.get' refering to the piece of code "Page.Clientscript;" Thanks.

推荐答案

我不确定我是否完全理解你尝试做的事情的顺序,什么是客户端,什么不是......

I'm not sure I fully understand the sequence of what you are trying to do, what's client-side and what's not....

但是,您可以向页面添加启动 javascript 方法,然后该方法将调用 WebMethod.通过javascript调用WebMethod时,可以添加一个回调函数,当WebMethod返回时调用.

However, you could add a Start-up javascript method to the page which would then call the WebMethod. When calling a WebMethod via javascript, you can add a call-back function, which would then be called when the WebMethod returns.

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

If you add a ScriptManager tag on your page, you can call WebMethods defined in the page via Javascript.

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

从 Page_Load 函数中,您可以添加对 WebMethod 的调用..

From the Page_Load function you can add a call to your WebMethod..

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

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

Callback_Function represents a javascript function that will be executed after the WebMethod is called...

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

找到此链接以了解 Web ADF 控件.这是你用的吗??

Found this link for Web ADF controls. Is this what you are using??

从那个页面来看,它看起来像这样会做一个 javascript 回调.

From that page, it looks like something like this will do a javascript callback.

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 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆