4

我在里面...

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;”谢谢。

4

4 回答 4

4

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

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

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

<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);
}
于 2008-09-30T16:32:23.837 回答
3

如果以上是您调用 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);          
    }
}
于 2008-09-30T17:05:37.687 回答
2

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

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

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

Page.RegisterStartupScript("ForceClientToCallServerMethod", script);

更多信息在这里

于 2008-09-30T16:32:58.703 回答
1

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

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

于 2008-09-30T16:27:00.307 回答