views:

3114

answers:

4

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.

+1  A: 

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

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

+4  A: 

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);
}
Scott Nichols
When will the alert fire? I need it to happen after a method has completed. In your example will the alert fire on page load?
mrjrdnthms
The alert would fire after the WebMethod was called. In this example, the sequence would go PageLoad (server) -> PageMethods call (javascript) -> DoSome WebMethod (server) -> Callback_Function (javascript).
Scott Nichols
You are awesome! Web ADF controls was exactly what I am using and your code worked like a charm. You went above and beyond. I love stack overflow because of people like you!
mrjrdnthms
+1  A: 

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

Will
Crap. Deprecated.
Will
Yeah when I put it in it underlines the whole Page.RegisterStartupScript line and says "An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.RegisterStartupScript'...
mrjrdnthms
+1  A: 

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);          
    }
}
Scott Nichols
So how would I incorporate my dropdown code into this?
mrjrdnthms