Skip to content Skip to sidebar Skip to footer

How Can I Call A Javascript Function From Inside A Method?

I am inside of... public class bgchange : IMapServerDropDownBoxAction { void IServerAction.ServerAction(ToolbarItemInfo info) { Some code... and after 'some code' I w

Solution 1:

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:ScriptManagerID="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...

<scriptlanguage="javascript"type="text/javascript">functionCallback_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.

publicvoidServerAction(ToolbarItemInfo info) {
    stringjsfunction="alert('Hello');";
    Mapmapctrl= (Map)info.BuddyControls[0];
    CallbackResultcr=newCallbackResult(null, null, "javascript", jsfunction);
    mapctrl.CallbackResults.Add(cr);
}

Solution 2:

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

Post a Comment for "How Can I Call A Javascript Function From Inside A Method?"