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

发布于 2024-07-06 14:17:17 字数 1383 浏览 6 评论 0原文

我在...里面

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;’,谢谢。

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. Is this possible?

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

}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北陌 2024-07-13 14:17:18

我不确定我是否完全理解您要执行的操作的顺序,什么是客户端,什么不是......

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

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

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

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

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

表示将在调用 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);
}

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. When calling a WebMethod via javascript, you can add a call-back function, which would then be called when the WebMethod returns.

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" />

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 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>

EDIT:

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

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);
}
清秋悲枫 2024-07-13 14:17:18

如果上面是您调用 RegisterStartupScript 的方式,它将不起作用,因为您没有对“Page”对象的引用。

您的示例中的 bgchange 扩展了 Object (假设 IMapServerDropDownBoxAction 是一个接口),这意味着没有 Page 实例可供您引用。

您可以从 Asp.Net Page 或 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);          
    }
}

If the above is how you are calling RegisterStartupScript, it won't work because you don't have a reference to the "Page" object.

bgchange in your example extends Object (assuming IMapServerDropDownBoxAction is an interface), which means that there is no Page instance for you to reference.

This you did the exact same thing from a Asp.Net Page or UserControl, it would work, because Page would be a valid reference.

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);          
    }
}
哥,最终变帅啦 2024-07-13 14:17:18

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

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

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

Page.RegisterStartupScript("ForceClientToCallServerMethod", script);

更多信息这里

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.

Within the ServerAction method, do the following

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

Page.RegisterStartupScript("ForceClientToCallServerMethod", script);

More info here

情何以堪。 2024-07-13 14:17:18

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

现在我想答案是否定的。 但我可能错了。

hmmm... the question changed dramatically since my original answer.

Now I think the answer is no. But I might be wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文