我如何从一个方法调用里面javascript函数? [英] How can I call a javascript function from inside a method?
问题描述
我里面......
public class bgchange : IMapServerDropDownBoxAction
{
void IServerAction.ServerAction(ToolbarItemInfo info)
{
Some code...
和后的一些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 code。显然,我不能正确实现它。目前VS说的对象引用是必需的非静态字段,方法或属性System.Web.UI.Page.ClientScript.get指的是那条code的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标签,你可以调用的WebMethods通过Javascript在网页中定义。
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重新presents将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屋!