ClientScript not working properly on class C#

2019-09-05 15:09发布

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?

1条回答
趁早两清
2楼-- · 2019-09-05 15:24

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);
    }
查看更多
登录 后发表回答