Skip to main content
 首页 » 编程设计

c#之非静态方法或字段错误需要一个对象

2024年02月05日15Terrylee

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

有

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

指定行错误。我该如何解决?

请您参考如下方法:

Page.ClientScript 不是静态属性,因此您不能以这种方式使用它。假设您有一个名为 pageInstance 的 Page 实例,可以在方法中访问它,请改用下面的方法:

ClientScriptManager cs = pageInstance.ClientScript;