ClientScript not working properly on class C#

梦想与她 提交于 2020-02-07 02:37:07

问题


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:


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


来源:https://stackoverflow.com/questions/29923961/clientscript-not-working-properly-on-class-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!