问题描述
我将使用下面的公共静态方法ClientScript功能(VS2010,C#),但它给了我一些错误(我想使用它的响应与_parent目标
I'm going to use following ClientScript function (VS2010,C#) in a public static method, but it gives me some errors (I want to use it for response redirect with "_parent" target
ClientScript.RegisterStartupScript(GetType(), "Load", "<script type='text/javascript'>window.parent.location.href = '" + a + "'; </script>");
Error 37 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.ClientScript.get'
Error 38 An object reference is required for the non-static field, method, or property 'object.GetType()'
感谢
推荐答案
您不能使用实例属性( ClientScript
)或方法(的GetType()
href=\"http://msdn.microsoft.com/en-us/library/aa645766%28v=vs.71%29.aspx\" rel=\"nofollow\">静一(基本上任何实例)。
You cannot use instance properties (ClientScript
) or methods (GetType()
) inside a static methods (basically anything instance).
删除static关键字,它应该工作:
Drop the static keyword and it should work:
public void SomeMethod()
{
ClientScript.RegisterSomeScript("Load",
"<script>....</script>");
}
评论后编辑:
或者,如果你需要,该方法是在静态类的静态传递Page对象作为参数:
Or if you need that the method is static in a static class pass the Page object as a parameter:
public static class ScriptRegistar
{
public static void RegisterSomeScript(Page page)
{
page.ClientScript.RegisterStartupScript("Load",
"<script>.........</script>");
}
}
使用(页面codebehind内):
Usage (inside a page codebehind):
public void Page_Load(Object sender, EventArgs e)
{
ScriptRegistar.RegisterSomeScript(this);
}
边注: ClientScript.RegisterStartupScript
有两个参数:脚本的键,脚本文本,所以没有必要在的GetType ()
那里。
Side note: ClientScript.RegisterStartupScript
takes two arguments: the key for the script, and script text, so there is no need for the GetType()
there.
这篇关于ASP.NET如何从公共静态方法调用clientscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!