How can I call a javascript function inside a method?

I'm inside...

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

and after "some code" I want to call

 [WebMethod] public static void DoSome() { } 

Which causes some javascript. Is it possible?

Ok, switch here. I managed to call dosome (); which fired but did not activate javascript. I tried using the registerstartupscript method, but I donโ€™t quite understand how to implement it. Here is 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 registerstartupscript code from msdn example. Clearly, I am not doing it right. Currently, vs says: "An object reference is required for a non-static field, method or property" System.Web.UI.Page.ClientScript.get "that refers to the code snippet of" Page.Clientscript ", thanks.

+4
source share
4 answers

I'm not sure I fully understand the sequence of what you are trying to do, what is on the client side and what is not ....

However, you could add JavaScript startup code to the page, which WebMethod then calls. When calling WebMethod through javascript, you can add a callback function that will then be called when WebMethod returns.

If you add the ScriptManager tag to your page, you can call WebMethods defined on the page through Javascript.

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

In 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 is a javascript function that will be executed after calling WebMethod ...

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

EDIT:

This link has been found for Web ADF Controls . Is that what you use?

From this 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); } 
+4
source

If you specify how you call RegisterStartupScript, this will not work because you do not have a link to the Page object.

bgchange in your example extends Object (it is assumed that IMapServerDropDownBoxAction is an interface), which means that there is no page to link to an instance.

You did the same from the Asp.Net or UserControl page, this will work because the page will be a valid link.

 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); } } 
+3
source

You cannot call methods in the browser directly from the server. However, you can add javascript functions to the response that will execute the methods in 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

+2
source

hmmm ... the question has changed since my initial answer.

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

+1
source

Source: https://habr.com/ru/post/1277325/


All Articles