Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 929
mrjrdnthms Avatar asked Dec 30 '22 09:12

mrjrdnthms


1 Answers

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);          
    }
}
like image 100
Scott Nichols Avatar answered Jan 02 '23 00:01

Scott Nichols