简体   繁体   English

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

[英]How can I call a javascript function from inside a method?

I am inside of... 我在里面......

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

and after "some code" I want to trigger 并且在“一些代码”之后我想触发

[WebMethod]
public static void DoSome()
{

}

Which triggers some javascript. 哪个触发了一些javascript。 Is this possible? 这可能吗?

Ok, switch methods here. 好的,在这里切换方法。 I was able to call dosome(); 我能够调用dosome(); which fired but did not trigger the javascript. 虽然解雇但没有触发javascript。 I have tried to use the registerstartupscript method but don't fully understand how to implement it. 我曾尝试使用registerstartupscript方法,但不完全了解如何实现它。 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);
        }
}

} }

I got the registerstartupscript code from an msdn example. 我从msdn示例中获得了registertartupscript代码。 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. 目前vs说“非静态字段,方法或属性'System.Web.UI.Page.ClientScript.get'需要一个对象引用,引用一段代码”Page.Clientscript;“谢谢。

I'm not sure I fully understand the sequence of what you are trying to do, what's client-side and what's not.... 我不确定我是否完全理解你要做什么的顺序,什么是客户端,什么不是......

However, you could add a Start-up javascript method to the page which would then call the WebMethod. 但是,您可以向页面添加一个启动javascript方法,然后调用WebMethod。 When calling a WebMethod via javascript, you can add a call-back function, which would then be called when the WebMethod returns. 通过javascript调用WebMethod时,可以添加一个回调函数,然后在WebMethod返回时调用该函数。

If you add a ScriptManager tag on your page, you can call WebMethods defined in the page via Javascript. 如果在页面上添加ScriptManager标记,则可以通过Javascript调用页面中定义的WebMethods。

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

From the Page_Load function you can add a call to your WebMethod.. 从Page_Load函数中,您可以添加对WebMethod的调用。

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

Callback_Function represents a javascript function that will be executed after the WebMethod is called... Callback_Function表示将在调用WebMethod后执行的javascript函数...

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

EDIT: 编辑:

Found this link for Web ADF controls . 找到此链接用于Web ADF控件 Is this what you are using?? 这是你正在使用的??

From that page, it looks like something like this will do a javascript callback. 从该页面看起来像这样的东西会做一个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);
}

If the above is how you are calling RegisterStartupScript, it won't work because you don't have a reference to the "Page" object. 如果以上是调用RegisterStartupScript的方法,那么它将无效,因为您没有对“Page”对象的引用。

bgchange in your example extends Object (assuming IMapServerDropDownBoxAction is an interface), which means that there is no Page instance for you to reference. 您的示例中的bgchange扩展了Object(假设IMapServerDropDownBoxAction是一个接口),这意味着您没有可供引用的Page实例。

This you did the exact same thing from a Asp.Net Page or UserControl, it would work, because Page would be a valid reference. 你从Asp.Net页面或UserControl做了完全相同的事情,它会工作,因为Page将是一个有效的参考。

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);          
    }
}

You cannot call methods within a browser directly from the server. 您无法直接从服务器调用浏览器中的方法。 You can, however, add javascript functions to the response that will execute methods within the browser. 但是,您可以将javascript函数添加到将在浏览器中执行方法的响应中。

Within the ServerAction method, do the following 在ServerAction方法中,执行以下操作

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

Page.RegisterStartupScript("ForceClientToCallServerMethod", script);

More info here 更多信息在这里

hmmm... the question changed dramatically since my original answer. 嗯......自从我的原始答案以来,问题发生了巨大的变化。

Now I think the answer is no. 现在我认为答案是否定的。 But I might be wrong. 但我可能错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM