2008-09-30 81 views
4

我里面......如何从一个方法内部调用一个javascript函数?

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

}

我拿到的RegisterStartupScript代码从MSDN的例子。很明显,我没有正确实施它。目前比较说:“非静态字段,方法或属性需要对象引用'System.Web.UI.Page.ClientScript.get'引用代码片段”Page.Clientscript;“谢谢。

+0

你能告诉我更多关于你想做什么吗?你的建筑和这样的?你想在更高层次上共谋什么? – 2008-09-30 16:14:40

+0

你是什么意思由DoSome()“触发一些JavaScript”? – 2008-09-30 17:25:06

回答

4

我不知道我完全理解你是什么样的顺序试图做什么,什么是客户端,什么是不...

但是,你可以添加一个启动JavaScript方法的页面,然后调用Web方法。当通过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 controls。这是你正在使用?

从该页面看起来像这样会做一个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); 
} 
+0

警报何时会触发?我需要它在方法完成后发生。在你的例子中,页面加载时会触发警报吗? – mrjrdnthms 2008-09-30 17:07:05

+0

WebMethod被调用后,警报将会触发。在这个例子中,序列将进入PageLoad(服务器) - > PageMethods调用(JavaScript) - > DoSome WebMethod(服务器) - > Callback_Function(javascript)。 – 2008-09-30 17:13:05

1

嗯......这个问题,因为我原来的答复发生了巨大变化。

现在我认为答案是否定的。不过,我可能是错的。

2

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

在了serveraction方法,请执行下列操作

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

Page.RegisterStartupScript("ForceClientToCallServerMethod", script); 

More info here

3

如果上面是你如何调用的RegisterStartupScript,它不会工作,因为你没有提到“页面“对象。

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);   
    } 
} 
相关问题