Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top