简体   繁体   中英

An object is required for the non-static method or field error

Type cstype = this.GetType();


        ClientScriptManager cs = Page.ClientScript;  ///Broken line


        if (!cs.IsStartupScriptRegistered(cstype, "loadvideo"))
        {
            StringBuilder cstext3 = new StringBuilder();
            cstext3.Append("jwplayer(\"vidplayer\").setup({");
            cstext3.Append("flashplayer:\"./players/player.swf\",");
            cstext3.Append("file: \"");
            cstext3.Append("./video.mp4");
            cstext3.Append("\",height: 270,");
            cstext3.Append("width: 400");
            cstext3.Append("});");
            cs.RegisterStartupScript(cstype, "loadvideo", cstext3.ToString(), true);

There is

"An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.ClientScript.get'"

error in specified line. How can I fix?

假设此代码来自页面本身,请尝试使用此代码...

ClientScriptManager cs = this.ClientScript;  ///Broken line

Page.ClientScript is not a static property, so you cannot use it this way. Assume you have the Page instance named as pageInstance which can be access in the method, use below instead:

ClientScriptManager cs = pageInstance.ClientScript;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM