0

I want to deploy a script on C# which usually uses methods like:

ClientScript.RegisterStartupScript(GetType(), "script", "Details('" + hdnId.Value + "');", true);

However I wanted to make a class that runs that code:

public class WebUtilities
    {
        public static void CustomScript(Page objPage, string strScript)
        {
            objPage.ClientScript.RegisterStartupScript(objPage.GetType(), "script", strScript, true);
        }
    }

When I call WebUtilities.CustomScript, sometimes it works, but leaves a //]]> at the bottom of the page.

And there is one occasion that it does not work at all. I only noticed that the first method works, and the second one doesn't.

How can I make the class version to work properly?

Victor
  • 1,108
  • 9
  • 29
  • 53

1 Answers1

1

I have this function, and it always works, try it

public static void callJavascriptFunction(string strScript)
    {

            if (HttpContext.Current == null && HttpContext.Current.Handler is Page) { return; }

            Page currentPage = (Page)HttpContext.Current.Handler;
            ScriptManager.RegisterStartupScript(currentPage,
                                                currentPage.GetType(),
                                                "Funct",
                                                strScript,
                                                true);
    }
Enrique Zavaleta
  • 2,098
  • 3
  • 21
  • 29
  • That would work if you put this code on the page, I want to use a method which is in a class, that way I can simply reuse it everywhere, instead of copying the same code to each page. – Victor Apr 28 '15 at 16:15
  • I have that code in a class on App_code folder, and it works, give it a try – Enrique Zavaleta Apr 28 '15 at 16:39
  • I tried it, and I get the following label at the bottom of the page. Sys.Application.initialize(); //]]> – Victor Apr 28 '15 at 17:10